natnai
Supremacy Member
- Joined
- Nov 6, 2007
- Messages
- 8,020
- Reaction score
- 1
Hey guys, I noticed these forums are really not very active, which is a shame. So I've decided to start this little thread for everyone who works with JavaScript in any form to share anything interesting that you come across in the course of your work/studies/whatever. Hopefully we can all help each other become better JavaScript developers 
To kick things off, I'll just write a small snippet on Prototypes in JS. They were very confusing for me when I first started, since I began by learning C++ (very basic) and then PHP, which I really liked.
As you all know, PHP follows a classical OO architecture, like Java. Javascript doesn't. Javascript utilises Prototypal Inheritance, which due to confusing operators like
, I think most programmers new to JS misunderstand.
Consider the following snippet:
Now consider this one:
The second snippet shows that Foo.prototype and Bar.prototype are actually the same object. That's because there are no classes in JavaScript whatsoever: there are only objects, and other objects are linked by reference to the prototypes of their ancestors. That's why assigning
to Bar.prototype.key also changed the value of Foo.prototype.key.
For me this was very frustrating, until I found out how JavaScript prototypes _actually_ works. If you want to COPY and not REFERENCE a prototype, then you should do:
That results in the assignment working as expected (see the first snippet). That's because Object.create copies the Foo.prototype in its entirety and assigns it to Bar.prototype, instead of just creating a reference through the internal [[Prototype]] property of Bar.
That's all from me! I hope other members will contribute as well, so we can all learn together. Cheers!
To kick things off, I'll just write a small snippet on Prototypes in JS. They were very confusing for me when I first started, since I began by learning C++ (very basic) and then PHP, which I really liked.
As you all know, PHP follows a classical OO architecture, like Java. Javascript doesn't. Javascript utilises Prototypal Inheritance, which due to confusing operators like
Code:
new
Consider the following snippet:
Code:
function Foo() {};
Foo.prototype.key = 'fooValue';
var Bar = Object.create(Foo);
Bar.prototype = Object.create(Foo.prototype);
Bar.prototype.key = 'barValue';
var BarProto = Object.getPrototypeOf(Bar);
console.log(BarProto);
console.log(Foo.prototype.key); // fooValue
console.log(Bar.prototype.key); // barValue
Now consider this one:
Code:
function Foo() {};
Foo.prototype.key = 'fooValue';
var Bar = Object.create(Foo);
Bar.prototype.key = 'barValue';
var BarProto = Object.getPrototypeOf(Bar); // Foo() {}
console.log(BarProto);
console.log(Foo.prototype.key); // barValue - wtf????
console.log(Bar.prototype.key); // barValue
The second snippet shows that Foo.prototype and Bar.prototype are actually the same object. That's because there are no classes in JavaScript whatsoever: there are only objects, and other objects are linked by reference to the prototypes of their ancestors. That's why assigning
Code:
'barValue'
For me this was very frustrating, until I found out how JavaScript prototypes _actually_ works. If you want to COPY and not REFERENCE a prototype, then you should do:
Code:
Bar.prototype = Object.create(Foo.prototype);
Bar.prototype.key = 'barValue';
That results in the assignment working as expected (see the first snippet). That's because Object.create copies the Foo.prototype in its entirety and assigns it to Bar.prototype, instead of just creating a reference through the internal [[Prototype]] property of Bar.
That's all from me! I hope other members will contribute as well, so we can all learn together. Cheers!
