I was playing around with JavaScript in particular simulating object oriented programming with classes and whatnot.
I knew about this way of achieving inheritance
MyClass.prototype = new AnotherClass();
But I wasn't satisfied, I didn't like how I needed to call the constructor of AnotherClass
. So I was playing around and I came up with something that seemed to work and basically want a second opinion.
function clone (obj)
{
function CloneFactory () {}
CloneFactory.prototype = obj;
return new CloneFactory();
}
MyClass.prototype = clone(AnotherClass.prototype);
By cloning the prototype we get a new copy of it and assign that to MyClass
's prototype so that changing the inherited properties will not affect the parent's prototype's properties. Like this would MyClass.prototype = AnotherClass.prototype
.
I ran stress tests and this is more efficient under certain circumstances, i.e. when there's a lot of code in the parent's constructor, otherwise it's about the same. Another benefit (or at least I find it to be beneficial) is that it allows to some extent information hiding from the subclasses. Any privileged methods and members will NOT be inherited.
Is there some major pitfall that I'm overlooking?
I'm not an expert with JavaScript, actually I'm fairly new to JavaScript, so I'd like to have a second opinion on this because I can't seem to find anything through Google. I don't want to implement bad code :)!
In JavaScript, an object can inherit properties of another object. The object from where the properties are inherited is called the prototype. In short, objects can inherit properties from other objects — the prototypes.
The Prototypal Inheritance is a feature in javascript used to add methods and properties in objects. It is a method by which an object can inherit the properties and methods of another object. Traditionally, in order to get and set the [[Prototype]] of an object, we use Object.
When it comes to inheritance, JavaScript only has one construct: objects. Each object has a private property which holds a link to another object called its prototype. That prototype object has a prototype of its own, and so on until an object is reached with null as its prototype.
prototype is a property of a Function object. It is the prototype of objects constructed by that function. __proto__ is an internal property of an object, pointing to its prototype. Current standards provide an equivalent Object.
This is almost exactly what Object.create
does. The function you've written is a pretty standard "polyfill" for that method.
This is a really common way of abstracting object creation in a way that more closely reflects "true" prototypal inheritance. Definitely a safe way to do things.
Oh, and here's a link to the MDN entry for Object.create
, if you're interested: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/create/
You'll notice at the bottom that they actually include the polyfill, which is pretty much identical to your code, save for some safety checks and variable names.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With