a life of coding

Friday, November 16, 2007

parent and __parent__ and __proto__, Oh My!

Sometimes Javascript development can be rather scary. Unlike C or Java, which have well known standards, Javascript (like HTML) is often described by what works on one or more of the current implementations. On top of that, few people see Javascript as a legitimate language, and a vast number of novices are out there writing code that would make your skin crawl.

In short, if you are a knowledgeable programmer, quickly picking up Javascript is much harder than it should be. The syntax is C-like (generally regarded as a plus), the functions are first class (almost universally positive), and the object system is prototype based (at least smalltalk people like this). Soon there will be a common just-in-time compiler, and some people have suggested that it might displace currently popular languages. Personally, I wish that it ditched the prototype object system for a Python-like one, and had lisp-like macros, and just-in-time compilation can't come soon enough.

When a question about a Javascript feature like "x.__proto__" arises, it can be difficult to answer. In my experience so far, the best places to look are MDC and the IRC channel listed on the page.

What is __proto__?

When you make a Javascript "class" (ie constructing function), you can specify a prototype:
function Example() {
this.foo = 1;
}

Example.prototype = {
sample : function memberFunction() {},
}
This attribute is copied onto constructed objects as __proto__, like so:
ex = new Example();
if (ex.__proto__ == Example.prototype) print("same!");
__proto__ is a special attribute that is searched if an attribute is not found on the object, it is analogous to a parent class.

parent and __parent__ are substantially different. parent comes from the DOM, and is the tag that encloses the current tag. __parent__ is a special variable that is a reference to the scope that created the object. Like __proto__ is checked when a variable lookup fails. Where __proto__ is checked when the failure looks like this.missing_attribute, __parent__ is checked when the failure is in the form missing_global. According to Mozilla, __parent__ and __proto__ have been deprecated, which is unfortunate since they can be quite useful.

3 Comments:

  • You can use "foo.constructor.prototype" as an undeprecated, partial replacement for __proto__. Of course, if foo.constructor, foo.constructor.prototype or foo.__proto__ are altered... At some point (soon for FF, ??? for others) we'll have the standard "Object.getPrototypeOf(foo)".

    As for the "parent" property, do you mean "parentNode"?

    By Anonymous medeis, At 9/11/08 11:20 AM  

  • yeah, I think it's the element.parentNode that points to the enclosing element, not element.parent.

    By Anonymous OsamaBinLogin, At 6/22/09 12:58 AM  

  • a few notes:

    Safari on the Mac, v4 and I'm pretty sure v3, also does __proto__ and you can even set it and do all the tricks.

    Firefox also has a __count__ pseudo-property that gives you the number of members specifically assigned into that object (not counting __proto__s).

    By Anonymous OsamaBinLogin, At 6/22/09 1:05 AM  

Post a Comment



<$I18N$LinksToThisPost>:

Create a Link

<< Home