1

I pass an object literal into a framework method called supportP(). This object literal has a special property called _p which denotes that the members of it are private. From with in the object literal it can be accessed via this._p. However when I pass the object literal into the "outer" scope I do not copy _p. It has now been made private by omission. In order to access _p from public member methods I bind them to the original object using bind() so they still have access to _p via this.

Will this work? Are there other things to consider? Wanted some feedback before I tested it out.

Below are relevant snippets.

/*$A.supportP
**
**
**
*/
$A.supportP = function (o, not_singleton) {
    var oo
        key;
    SupportList[o.Name] = {};
    if (not_singleton) {
        // ignore this section
    } else { // *look here - isFunc returns true if a function
        for (key in o) {
            if ((key !== '_p') && (isFunc(o[key])) {
                oo[key] = o[key].bind(o);
            } else if (key !== '_p') {
                oo[key] = o[key];
            } else {
                // private (_p) - anything to do here?
            }
        }
        return oo;
    }
};


/*$A.test
**
**
**
*/
var singleton_object = $A.supportP({
    _p: 'I am private',
    Name: 'test',
    publik_func: function () {
        // this will refer to this object so that it can access _p
        // this._p is accessible here due to binding
    }
}, false);
  • Either use the dot or square brackets as [member operators](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Member_Operators), but not both – Bergi Jan 23 '13 at 23:23
  • 1
    Why do you want to do this? Trust the developer not to mess with your privates. @Bergi: He seems to be using the [] notation only if the property name is dynamic and `.` in other cases. So he does it exactly as it should be. – ThiefMaster Jan 23 '13 at 23:28
  • 1
    "Will this work" questions can usually be confirmed with a simple test case - have you tried it? – AlienWebguy Jan 23 '13 at 23:30

1 Answers1

1

Will this work?

Yes, you will be able to access the "private" property via this._p.

Are there other things to consider?

You are cloning the object. Yet, the method on it has no access to it - it is bound to the "old" object whose properties will not reflect the changes on the copy. I am not sure whether this is by design or by accident.


For strict privateness, you will need to use closures with local variables. Properties can never be made private.

var singleton_object = (function() {
    var _p = 'I am private'; // local variable
    return {
        Name: 'test',
        publik_func: function () {
            // this will refer to this object so that it can access the properties
            // _p is accessible here due to closure, but not to anything else
        }
    };
}()); // immediately-executed function expression

Another solution, using two distinct objects (one hidden) which are passed into a framework method:

function bindPrivates(private, obj) {
    for (var key in obj)
        if (typeof obj[key] == "function")
            obj[key] = obj[key].bind(obj, private);
    return obj;
}

var singleton_object = bindPrivates({
    p: 'I am private'
}, {
    Name: 'test',
    publik_func: function (_) {
        // this will refer to this object so that it can access "public" properties
        // _.p, a "private property" is accessible here due to binding the private 
        //  object to the first argument
    }
});
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • No, you are copying primitive values - not references. Keeping them fresh via getters/setters sounds like a bad idea to me. What were you originally trying to do? – Bergi Jan 23 '13 at 23:42
  • ....http://stackoverflow.com/questions/3474697/how-to-clone-js-object...just making sure... –  Jan 23 '13 at 23:47
  • Why not use closures? (It's the only possibility, and `bind` does a similar thing) JavaScript just is no classical language. – Bergi Jan 23 '13 at 23:56
  • @pure_code: Depends on what the function does with the object (a local argument) :-) Btw, there are no "anonymous objects" – Bergi Jan 24 '13 at 00:00
  • functions can have names (or not), but I never heard that terminology for objects. You always assign them to variables or pass them to functions (where they usually are referenced via named parameter variables). – Bergi Jan 24 '13 at 00:06
  • No, there are no references in JS or operators for that. Btw, even objects are values (which reference their property map in the memory) – Bergi Jan 24 '13 at 08:10
  • There are references, and I can get around the primitive problem, by storing all my primitives in a container. Here is the first google hit I got on pass by reference vs. pass by values - http://snook.ca/archives/javascript/javascript_pass –  Jan 24 '13 at 21:40
  • That article is wrong. All javascript variables are passed by value. – Bergi Jan 24 '13 at 21:43
  • p.45, Definitive JavaScript, by Flanagan, Latest Edition. Explains `by reference`. –  Jan 25 '13 at 00:16