Naked JavaScript objects
I was looking through one of the source files of Narcissus, the JavaScript interpreter written in JavaScript, when I came across a line that I probably missed before:
var keywords = {__proto__: null};
The __proto__ property is only accessible in Firefox, Safari, Rhino, and ActionScript and is the property that ties an object instance to its prototype. A little-known fact about JavaScript is that object instances have no relationship to their constructors, only to their prototypes. The __proto__ property exposes this relationship.
As with most properties, __proto__ can be overwritten. Doing so changes the prototype chain of the object. The code from Narcisuss effectively creates a JavaScript object that has no prototype chain and therefore none of the methods that all objects inherit from Object. The result? A truly naked base object that has no properties. Cutting off the prototype chain also ensures that changes to Object.prototype won’t effect the use of for-in. This is really interesting:
var o = { __proto__: null };
alert(o.toString); //undefined
Interestingly, this seems to be the only way to create a naked object. You can define a constructor whose prototype is set to null, but creating an instance using that constructor automatically resets the prototype to Object. Example:
function MyObject(){}
MyObject.prototype = null;
var o = new MyObject();
alert(o.toString); //function
I don’t know that this information is useful in any way, but it certainly is interesting.
Disclaimer: Any viewpoints and opinions expressed in this article are those of Nicholas C. Zakas and do not, in any way, reflect those of my employer, my colleagues, Wrox Publishing, O'Reilly Publishing, or anyone else. I speak only for myself, not for them.
Both comments and pings are currently closed.




4 Comments
Nicholas,
It seems there is a misprint in the following phrase and:
"…object instances have no relationship to their constructors, only to their prototypes."
the combination was intended to be inverse
"…object instances have no relationship to their PROTOTYPES, only to their CONSTRUCTORS."
because specification defines constructor property on the Object prototype object and it is widely known and used. It is relation object –> prototype that is hidden and exposed only by the mentioned engines.
Alexei on July 11th, 2008 at 5:52 am
Actually, there is no misprint. Object instances only have a relationship to the prototype, not to the constructor. The prototype has a relationship to the constructor via the constructor property, which object instances have access to through the prototype chain. But there is no direct connection between the object instance and its constructor.
Nicholas C. Zakas on July 12th, 2008 at 1:33 am
Very interesting.
There is a misprint by the way )
In the example:
var o = { __proto__: null }–>}<–;
Eugene Fedin on August 11th, 2008 at 8:28 am
Thanks, I’ve fixed it.
Nicholas C. Zakas on August 13th, 2008 at 11:45 pm
Comments are automatically closed after 14 days.