MollyPages.org
"You were wrong case. To live here is to live." |
Pages /
Database /
Forms /
Servlet /
Javadocs
![]() ![]() |
function Foo() { } ; var f1 = new Foo(); Foo.prototype.x = "hello"; f1.x //=> hello Foo.x //=> undefinedNote, we use the
Foo
.prototype to set properties
for all objects created by function Foo. We don't say
f1
.prototype to set properties for f1
.
This is a very important point to remember.
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
Setting the same property via the object shadows/hides the same property in the prototype for that instance.
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.Setting the property directly in the prototype changes it for all instances.
foo.prototype.x = "goodbye"; //now f1.x => "goodbye" f2.x => "goodbye";
Function.constructor === FunctionThat is to say: Function is it's own constructor !
This is because:
Object.__proto__.__proto__.constructor == Object
Note 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.
f1.toString() finds:
Object.prototype.toString
We get something like: [object ...]
Whereas:
Foo.toString() first finds & uses: Function.prototype.toString() We get something like: [Function foo...]If we say:
delete Function.prototype.toString Foo.toString() We get something like: [object ...]