The whole concept of prototyping takes some time to fully understand but here are some common pitfalls:
Forgetting to reset the constructor property after assigning a prototype object:
var Foo() = function ()
{
this.batz = '...';
};
Foo.prototype = new Bar();
Foo.prototype.constructor = Foo;
If you forget the least line, new Foo()
will actually execute Bar()
.
Another pitfall with prototyping is iterating over objects/arrays without filtering out the members of the prototype:
for (var i in obj) {
if (obj.hasOwnProperty(i)) {
//stuff...
}
}
The extra condition will skip any members that are inherited from the prototype of obj
.