2 ms·
Many people have tried to make javascript function with traditional OO inheritance, which makes me wary. Do people try to do this because they find the prototyp
by code_duck 16y ago
Many people have tried to make javascript function with traditional OO inheritance, which makes me wary. Do people try to do this because they find the prototypal model unfamiliar? It seems so.
I was hesitant to get into OO style in PHP and even Python, because it seemed to me to be over-applied. While classic inheritance and objects are useful, they're not the answer to everything, as most readers of HN probably agree. I'm glad to see more people embracing JS's prototypal nature.
- olavk 16y agoOne reason is perhaps that the prototypal patterns has not been that well supported in JavaScript. For example the use of constructor functions and the "new" operator parallels classes in traditional OO. A perhaps more "prototypal" approach would be to define instances by using object literal syntax. But the object literal syntax does not allow one to define the prototype, so it is more limited than using constructors. One of the proposed extensions in the abandoned ECMAScript 4 proposal was to allow prototype definition in object literals, like: var obj = { x : 10, __proto__: protoobj }; This would make prototypal inheritance more natural, since you don't need the constructor as an intermediate step. ECMAScript 5 has the method Object.create which creates a new instance with the given prototype: var obj = Object.create(protoobj, {x: 10}); But this is not yet widely supported, while "new" has been in the core since day one. Basically the JavaScript language has been slow to fully embrace its prototypal nature.
- code_duck 16y agoTrue, the object instantiation syntax has been bizarre from the beginning. That's created a lot of confusion and workarounds.