L
Luke Matuszewski
I have pointed some example of oop in JavaScrip; Here it is:
<quote>
---
function Foo(){};
function Bar(){};
var foo = new Foo();
var bar = new Bar();
Foo.prototype.baz = 42;
alert(foo.baz); // alerts 42! the prototype link is live
Bar.prototype = Foo.prototype; /* [AAA] */
alert(bar.baz); // alerts undefined. Inheritance is between objects,
// the function is not important afterwards.
alert(foo instanceof Bar); // alerts true - really tests if
// Bar.prototype is in foo's prototype
chain.
// Again, the function itself is not
important
---
/L
[1] Ok, there *is* one "root" object that doesn't have a prototype:
It's available as Object.prototype.
---
</quote>
[AAA] ... here the 'old' Bar.prototype object (which is added to all
functions/constructors in JavaScript) is 'droped' and new reference to
Foo.prototype object is assigned...but 'bar' object was created before
that assigment so 'old' Bar.prototype object is used for prototype
inheritance (so bar.baz is undefined).
PS i want to ask a question about objects/closures in JS...
1. Does from version JavaScript1.0 the 'new' keyword was working as it
is here in new versions of JavaScript... ?
2. Can anyone point some 'buggy' behaviour of 'this' keyword when used
in constructor/constructor inside functions and constructor outside
functions ? I have heard that it is wise to use a reference to is on
top of construcotr body:
/* constr. body */
var this.thisRef = this;
Thanks in advance for answers
<quote>
---
function Foo(){};
function Bar(){};
var foo = new Foo();
var bar = new Bar();
Foo.prototype.baz = 42;
alert(foo.baz); // alerts 42! the prototype link is live
Bar.prototype = Foo.prototype; /* [AAA] */
alert(bar.baz); // alerts undefined. Inheritance is between objects,
// the function is not important afterwards.
alert(foo instanceof Bar); // alerts true - really tests if
// Bar.prototype is in foo's prototype
chain.
// Again, the function itself is not
important
---
/L
[1] Ok, there *is* one "root" object that doesn't have a prototype:
It's available as Object.prototype.
---
</quote>
[AAA] ... here the 'old' Bar.prototype object (which is added to all
functions/constructors in JavaScript) is 'droped' and new reference to
Foo.prototype object is assigned...but 'bar' object was created before
that assigment so 'old' Bar.prototype object is used for prototype
inheritance (so bar.baz is undefined).
PS i want to ask a question about objects/closures in JS...
1. Does from version JavaScript1.0 the 'new' keyword was working as it
is here in new versions of JavaScript... ?
2. Can anyone point some 'buggy' behaviour of 'this' keyword when used
in constructor/constructor inside functions and constructor outside
functions ? I have heard that it is wise to use a reference to is on
top of construcotr body:
/* constr. body */
var this.thisRef = this;
Thanks in advance for answers