7

EDIT: I figured it out from Bergi's answer in the end.

Thanks Bergi.

pubPrivExample = (function () {
    return {
        init : function () {
            var private;

            this.setPrivate = function (p) {
                private = p;
            };
            this.getPrivate = function () {
                return private;
            };
        },

        public : "This is public\n"
    };
}());

var a;

a = Object.create(pubPrivExample);
a.init();
a.setPrivate("This is private");

document.write(a.getPrivate());

EDIT: It seems the answers to my question are off at a tangent. I'm really not interested in a factory and actually would rather not use if. My question is about private state. From Bergi's answers and comments I think I can pull something together.

To be continued...

EDIT: Bergi has started to answer the question below, but left out the most important part - the private state.

I have had time to think about the idea more, but am still unable to achieve private state using Object.create() without some kind of factory. But I want to be wrong, and Bergi alluded to a solution... Feel free to take Bergi's answer as a starting point.

ORIGINAL: My quest to avoid new in javascript has lead me to a peculiar place. I want private object members, but I don't want to give up Object.create().

Here's the code.

var trackQueue = {};

trackQueue.factory = function () {
    var that, queue;
    that = this;
    queue = [];

    that.push = function (item) {
        queue.push(item);
    };

    that.work = function () {
        document.write(queue + "<br />");
    };

    return {
        work : that.work,
        push : that.push
    };        
};

var a = Object.create( trackQueue.factory() );
a.push("a");
a.push("b");
a.push("c");

var b = Object.create( trackQueue.factory() );
b.push("d");
b.push("e");
b.push("f");

a.work();
b.work();

And a jsfiddle

http://jsfiddle.net/dsjbirch/Wj6cp/10/

Would init be a more idiomatic / appropriate name for the factory method?

Is this insane?

Be kind - javascript isn't my first language.

bluekeys
  • 2,217
  • 1
  • 22
  • 30

2 Answers2

2

Yes, a init method on the prototype might be a more appropriate name:

var proto = {
    init: function(args) {
        // setting up private-scoped vars,
        var example = args;
        // privileged methods
        this.accessPrivate = function(){ return example; };
        // and other stuff
        this.public = 5;
    },
    prop: "defaultvalue",
    ...
}

var instance = Object.create(proto);
instance.init();

However, there is absolutely no reason not to use the classical constructor with the new keyword, which elegantly combines the Object.create and init call.

And note that you are using Object.create with absolutely no use. Your factory pattern (perfectly valid applied) returns good objects. No need to create new objects for each one that inherit from them. Just do:

var instance = trackQueue.factory();

If you like the sound of the method name "create", you might use a more idiomatic name for your factory:

trackQueueFactory.create = function(args) {...};

EDIT: Your idea to combine the factory pattern with prototype inheritance is not so wrong. Yet, the proto object from which all the fabricated objects inherit needs to be static, instead of creating a new one on each invocation. Your code might look like this:

var factory = {
    proto: {
        ...
    },
    create: function(args) {
        var product = Object.create(this.proto);
        // set up private vars scoped to the create function
        // privileged methods
        product.doSomethingSpecial = function(){ ... };
        // and other stuff
    }
};

var a = factory.create(...);
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
2

I think this is a clear way to achieve your requirements:

var personFactory = function(id, name, age){
    var _id = id;
    var _name = name;
    var _age = age;

    var personPrototype = {
        getId: function(){
            return _id;
        },
        setId: function(id){
            _id = id;
        },
        getName: function(){
            return _name;
        },
        setName: function(name){
            _name = name;
        },
        getAge: function(){
            return _age; 
        },
        setAge: function(age){
            _age = age;
        },
        work: function(){
            document.write(this.toString());
        },
        toString: function(){
            return "Id: " + _id + " - Name: " + _name + " - Age: " + _age;
        }
    };

    return Object.create(personPrototype);
};

Usage:

var renato = personFactory(1, "Renato Gama", 25);
console.log(renato.getName()); //logs "Renato Gama"
renato.setName("Renato Mendonça da Gama");
console.log(renato.getName()); //logs "Renato Mendonça da Gama"

If I am not wrong this is one the MODULE PATTERN usages. Refer to this post for a nicer explanation. This is also a good post about the subject.

Renato Gama
  • 16,431
  • 12
  • 58
  • 92
  • No, you are wrong. You've made the same error as the OP: There is absolutely no need to use Object.create on private-scope-factored objects. – Bergi Jul 02 '12 at 19:04
  • I can't get your point, can you clarify a little bit? Can't see why its considered an error. I even consider your second code block similar the one I wrote, differing on the fact that your prototype is a public member of the factory. I agree with your approach if your factory object would have more public functions but the create() one. – Renato Gama Jul 02 '12 at 22:40
  • Why don't you return the object literal ("personPrototype") directly? Of what use is Object.create in your code? – Bergi Jul 03 '12 at 10:51
  • The purpose is to return an instantiated object, with properties already set from within the factory (which works like a constructor method). In other words, to encapsulate initiation logic. – Renato Gama Jul 03 '12 at 12:13
  • Yes, it's exactly doing that when returning the "personPrototype" object. You know what [Object.create](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/create/) does? – Bergi Jul 03 '12 at 12:22
  • Great answer, I would simlpy remove Object.create as others have suggested, and isntead just return the personPrototype. – Dave Voyles Sep 15 '15 at 23:31