In JavaScript what is the difference between these two examples:
Prerequisite:
function SomeBaseClass(){
}
SomeBaseClass.prototype = {
doThis : function(){
},
doThat : function(){
}
}
Inheritance example A using Object.create:
function MyClass(){
}
MyClass.prototype = Object.create(SomeBaseClass.prototype);
Inheritance example B using the new keyword
function MyClass(){
}
MyClass.prototype = new SomeBaseClass();
Both examples seem to do the same thing. When would you chose one over the other?
An additional question: Consider code in below link (line 15), where a reference to the the function's own constructor is stored in the prototype. Why is this useful?
https://github.com/mrdoob/three.js/blob/master/src/loaders/ImageLoader.js
Excerpt (if you don't want to open the link):
THREE.ImageLoader.prototype = {
constructor: THREE.ImageLoader
}
I am not an expert in java script but here is a simple example to understand difference between "Object.create" and "new" ..
step 1 : create the parent function with some properties and actions..
step 2 : create a child function (PersonSalary) which extends above Person function using New keyword..
step 3 : create second child function (PersonLeaves) which extends above Person function using Object.create keyword..
// Now check both child functions prototypes.
both of these child functions will link to Person(parent function) prototype and can access it's methods but if you create child function using new it will return a brand new object with all parent properties which we don't need and also when you create any object or function using "New" that parent function is executed which we don't want to be.
Here are the takeaways
In your question you have mentioned that
Both examples seem to do the same thing
, It's not true at all, becauseYour first example
In this example, you are just inheriting
SomeBaseClass' prototype
but what if you have a property in yourSomeBaseClass
likeand if you use it like
The
obj
object won't havepublicProperty
property like in this example.Your second example
It's executing the
constructor
function, making an instance ofSomeBaseClass
and inheriting the wholeSomeBaseClass
object. So, if you useIn this case its
publicProperty
property is also available to theobj
object like in this example.Since the
Object.create
is not available in some old browsers, in that case you can useAbove code just adds
Object.create
function if it's not available so you can useObject.create
function and I think the code above describes whatObject.create
actually does. Hope it'll help in some way.The difference becomes obvious if you use
Object.create()
as it is intended. Actually, it does entirely hideout theprototype
word from your code, it'll do the job under the hood. UsingObject.create()
, we can go likeAnd then we can extend/inherit other objects from this
So this is another concept, a more "object oriented" way of inherting. There is no "constructor function" out of the box using
Object.create()
for instance. But of course you could just create and call a self defined constructor function within those objects.One argument for using
Object.create()
is that it might look more natural to mix/*inherit* from other objects, than using Javascripts default way.That's true in your case.
When
SomeBaseClass
has a function body, this would get executed with thenew
keyword. This usually is not intended - you only want to set up the prototype chain. In some cases it even could cause serious issues because you actually instantiate an object, whose private-scoped variables are shared by allMyClass
instances as they inherit the same privileged methods. Other side effects are imaginable.So, you should generally prefer
Object.create
. Yet, it is not supported in some legacy browsers; which is the reason you see thenew
-approach much too frequent as it often does no (obvious) harm. Also have a look at this answer.