Attempting to understand privileged members with Object.create

This is in reference to Crockford’s articles found here:

http://javascript.crockford.com/prototypal.html
http://javascript.crockford.com/private.html

I’m attempting to understand the use of privileged methods when used with Object.create. I put together a quick demo (code below) that shows what happens when I use Object.create to create a new object based on one that has a privileged method. The outcome is not pleasant, as changing the value in the first object also changes it in the second. Unless I am reading Crockford’s articles incorrectly, this makes Object.create almost useless for anything but objects that have only public members.

I’ve seen many JavaScript programmers use closures extensively to create private members, and that still holds to be a good programming practice. However I still can’t find an elegant way to create inheritance in combination with closures in JavaScript, given downfalls such as the one I mentioned above.

With all of that said I still think Crockford has a nice way of programming, creating factory functions that produce objects, staying away from the prototype property and making the language look more functional.

Here’s the code to demonstrate what I’m referring to. Firebug needs to be enabled to view the console.debug output, otherwise convert them to alerts.


if (typeof Object.create !== 'function') {
    Object.create = function (o) {
        function F() {}
        F.prototype = o;
        return new F();
    };
}

var o = (function() {
	var msg = "hi";
	
	return {
		msg: function(m) {
			if (m === undefined) {
				return msg;
			}
			
			msg = m;
		}
	};
})();

var foo = Object.create(o);
var bar = Object.create(foo);

console.debug(foo.msg());	// "hi"

foo.msg("what?");

console.debug(foo.msg());	// "what?"
console.debug(bar.msg());	// "what?"

Hi,

I think it’s because the prototype object is shared. That’s the whole purpose of prototype - instances of the object all share the same properties of the prototype object.
What you’re trying to achieve can be a lot simpler but I don’t think this is what you are after:

var o = function() {
	var msg = "hi";
	this.msg = function(m) {
		if (typeof m != 'undefined') {
			msg = m;
		}
		return msg;
	};
};
var foo = new o();
var bar = new o();

console.debug(foo.msg());	// "hi"

foo.msg("what?");

console.debug(foo.msg());	// "what?"
console.debug(bar.msg());	// "hi"

I appreciate your response, but I’m not looking for a classical view on this approach (refer to the articles I linked above). In addition they have the same value because of the closure. In your example you created foo and bar from o, so of course there will be no problems since they have separate closures around the msg variable. My example is using inheritance, creating foo from o, and bar from foo. In this situation a problem occurs when attempting to use privileged variables as discussed by Crockford.

Hmm, your example doesn’t represent any form of inheritance I’ve ever studied.