3 ms·
In prototype-based OO, a constructor is just a function that builds an object. If you like, you can give that function a starting point for building its objec
by jarek-foksa 14y ago
In prototype-based OO, a constructor is just a function that builds an object.
If you like, you can give that function a starting point for building its objects
(the prototype), but in the end, it's just a function that builds an object.
Isn't this called pseudo-classical inheritance?
In purely prototypical inheritance one object inherits from another object, there is no need for constructors whatsoever,
instead you are just setting __proto__ link so that it points to the object from which you want to inherit.
This way you have much more freedom - you could add initializer method that sets up variables in a similar fashion to constructor, but you could also easily implement singleton or mixin patterns.
Ideally, prototypical inheritance would be done like in the code below, but I doubt we will ever see this supported by JS:
obj1 = {
__proto__: Object.prototype,
init: function() {
}
}
obj2 = {
__proto__: obj1,
init: function() {
super.init()
}
}
obj3 = {}
obj3.__proto__ = obj2
obj3.init()