Interesting Points
- All instances inherit from the prototype object of the function that created them.
- Mozilla/Konqueror have an implementation-specific __proto__ property that points to the prototype object of the creator function (the function used to create any instance of that type).
- Regardless of the presence/absence of a __proto__ property, the general idea is that all objects use the prototype object pointed to by that objects creator function. This property is part of the Javascript standard and is called prototype. The prototype object, by default, has a constructor property pointing back to the function that it's the prototype for.
- The prototype is only used for properties inherited by objects/instances created by that function. The function itself does not use the associated prototype (but since the function itself is an object, it inherits from the prototype of it's creator function, typically the javascript system "Function" object).
Note, we use thefunction Foo() { } ; var f1 = new Foo(); Foo.prototype.x = "hello"; f1.x //=> hello Foo.x //=> undefined
f1 = new Foo(); Foo.prototype.x = "hello"; f1.x //=> hello Foo.x //=> undefinedFoo
.prototype to set properties for all objects created by function Foo. We don't sayf1
.prototype to set properties forf1
. This is a very important point to remember. - Default prototype objects can be replaced with another user created object. While doing so, the constructor property must be set manually to replicate what the javascript runtime does behind the scence with the default prototype object.
function foo() { } ; var f1 = new foo(); f1.constructor === foo.prototype.constructor === foo //replace the default prototype object foo.prototype = new Object(); //create another instance l f1 = new foo(); //now we have: f1.constructor === foo.prototype.constructor === Object //so now we say: foo.prototype.constructor = foo //all is well again f1.constructor === foo.prototype.constructor === foo
f1 = new foo(); f1.constructor === foo.prototype.constructor === foo //replace the default prototype object foo.prototype = new Object(); //create another instance l f1 = new foo(); //now we have: f1.constructor === foo.prototype.constructor === Object //so now we say: foo.prototype.constructor = foo //all is well again f1.constructor === foo.prototype.constructor === foo - Each prototype object itself is created (by default) with the Object() constructor, hence the prototype has as it's prototype Object.prototype. Therefore all instances regardless of the type ultimately inherit properties from Object.prototype.
- All objects automatically read properties in the prototype chain as-if those properties where defined in the object itself.
Setting the same property via the object shadows/hides the same property in the prototype for that instance.
Setting the property directly in the prototype changes it for all instances.function foo() { } f1 = new foo(); f2 = new foo(); foo.prototype.x = "hello"; f1.x => "hello" f2.x => "hello"; f1.x = "goodbye"; //setting f1.x hides foo.prototype.x f1.x => "goodbye" //hides "hello" for f1 only f2.x => "hello" delete f1.x f1.x => "hello"; //foo.prototype.x is visible again to f1.
f1 = new foo(); f2 = new foo(); foo.prototype.x = "hello"; f1.x => "hello" f2.x => "hello"; f1.x = "goodbye"; //setting f1.x hides foo.prototype.x f1.x => "goodbye" //hides "hello" for f1 only f2.x => "hello" delete f1.x f1.x => "hello"; //foo.prototype.x is visible again to f1.foo.prototype.x = "goodbye"; //now f1.x => "goodbye" f2.x => "goodbye";
//now f1.x => "goodbye" f2.x => "goodbye";
More Interesting points
Viewing/following various arrows in the diagram above, you'll see some interesting relationships in the core javascript language. (Type these code examples in a javascript console if you want to play along).
- Function.__proto__ points to Function.prototype. This results in:
Function.constructor === Function
That is to say: Function is it's own constructor ! - Object instanceof Object == true.
This is because:
Object.__proto__.__proto__.constructor == ObjectNote also that unlike
Object instanceof Object
, Foo instanceof Foo == false.This is because: Foo does not exist as a constructor for it's own prototype chain.
- Function.prototype.toString is a built-in method and is distinct from another built-in method: Object.prototype.toString
Whereas:f1.toString() finds: Object.prototype.toString We get something like: [object ...]
.toString() finds: Object.prototype.toString We get something like: [object ...]
If we say:Foo.toString() first finds & uses: Function.prototype.toString() We get something like: [Function foo...]
delete Function.prototype.toString Foo.toString() We get something like: [object ...]