Which parameter should i pass for the first parent object from which others will inherit and which one is more efficient
Object.create(Object.prototype)
Object.create(Object)
Object.create(null)
Object.create(null)
returns an empty object
Object.create(Object)
returns a function why????( I checked my log and it says function...i used console.dir() )
Object.create(Object)
returns a non empty object
How does this whole thing work ... I m more used to the Classname .prototype thing :(
Can't understand what is going on here
prototype while Object. create(null) doesn't inherit from anything and thus has no properties at all. In other words: A javascript object inherits from Object by default, unless you explicitly create it with null as its prototype, like: Object. create(null) .
The major difference is that Object. Create returns the new object while the constructor function return the constructor of the object or the object. This is due to the important difference that new actually runs constructor code, whereas Object. create will not execute the constructor code.
Object with null prototype and its purpose If you create an object in JavaScript - it has some methods by default, such as toString(), keys() or hasOwnProperty(). But that is true when you create an object based on default Object.
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.
The Object.create () method creates a new object, using an existing object as the prototype of the newly created object. Object.create(proto) Object.create(proto, propertiesObject) The object which should be the prototype of the newly-created object.
Object.create() methord is used to create a new object with the specified prototype object and properties. Object.create() method returns a new object with the specified prototype object and properties. Applications: Object.create() is used for implementing inheritance.
The fundamental differences between Object.assign () and Object.create () Object. assign (): Object. assign () is used to copy enumerable properties ( properties which can be iterated or which have enumerable property set to true check this!! ) from a source object to a target object.
Object.create () is used for implementing inheritance. prototype : It is the prototype object from which a new object has to be created. propertiesObject : It is optional parameter. It specifies the enumerable properties to be added to the newly created object.
Preface: JavaScript uses prototypical inheritance, which means that an object can have (usually does have) a prototype behind it, which is another object. If you try to get the value of a property from an object that it doesn't have, the JavaScript engine looks to the object's prototype (and its prototype, and so on) to find it.
Object.create
creates objects. The first argument you give Object.create
is the object to use as the prototype of the object it creates. So:
// Create an object with a property 'foo'
var a = {
foo: 42
};
// Create a blank object using `a` as its prototype
var b = Object.create(a);
// Give `b` a property of its own
b.bar = "hi";
That gives us this in memory:
+−−−−−−−−−−−−−−−+ +−−−−−−−−−−−−−−−−−−−+ | [[Prototype]] |−−−−−>| (the standard | a−−−−−−−−−−−−−−−−−−−−−−+−−>| foo: 42 | | object prototype) | | +−−−−−−−−−−−−−−−+ +−−−−−−−−−−−−−−−−−−−+ | +−−−−−−−−−−−−−−−+ | b−−>| [[Prototype]] |−−+ | bar: "hi" | +−−−−−−−−−−−−−−−+
Proof b
uses a
:
console.log(b.foo); // 42
a.foo = 67;
console.log(b.foo); // 67
Addressing some of your variations:
var o = Object.create(Object.prototype);
That's exactly the same as var o = {};
: It creates a new blank object whose prototype is the object Object.prototype
references.
var o = Object.create(Object);
That creates a new blank object o
whose prototype is the Object
function. It doesn't create a function, just a non-function object that has a function as its prototype. This would be quite odd and probably isn't what you want. :-)
var o = Object.create(null);
Creates a new blank object o
whose prototype is null
. Since its prototype is null
, it doesn't have the usual Object.prototype
stuff, like toString
and valueOf
and hasOwnProperty
. That's a bit unusual, although there are use cases for it, such as when you're using an object as a dictionary/map and don't want false positives for those property names. (In ES2015 [aka ES6] another option is to use Map
instead.)
As thg435 points out in a comment below, one of the confusing things about JavaScript is that the prototype of an object is a completely different thing from the prototype
property you see on functions. It would probably be better if the prototype
property had had a different name (although I can't imagine what name it would be without being massively clunky).
An object (let's call it o
) has a prototype object it inherits properties from. (In ES2015+ you can get access to that object via Object.getPrototypeOf
.) The object on the prototype
property of functions is not necessarily the prototype of any object at all. Instead, it's the object that will be assigned as the prototype of any object created via new
using that function.
Examples help here.
function Foo() {
}
That function, Foo
, has a property Foo.prototype
that refers to an object. That object is not, yet, used as the prototype of anything. It's just an object assigned to a property called prototype
on the Foo
object instance.
var f = new Foo();
Now that object is used as a prototype, specifically it's the prototype of the f
object created by the new Foo
call.
Ignoring a couple of details, this line of code:
var f = new Foo();
...basically does this:
// Create a blank object, giving it `Foo.prototype` as its prototype
var f = Object.create(Foo.prototype);
// Call` Foo` using that new object as `this`
Foo.call(f);
As I say, that leaves out a couple of details, but hopefully it helps make it clear what the prototype
property of functions is for...
What is being returned is an object.
>>> typeof Object.create(Object)
<<< "object"
>>> Object.create(Object)
<<< Function {}
// ^^
Function
is the name which Chrome addresses the object's constructor. See How are javascript class names calculated for custom classes in Chrome Dev Tools?
This part of the answer addresses @phenomnomnominal's comment in the question, explaining why the created object has inherits function properties such as call
.
The Object
constructor is a function, and thus inherits from the Function
prototype:
>>> Object.call === Function.prototype.call
<<< true
So an object having Object
as prototype will have a link to the Function prototype via prototype chain as well:
>>> Object.create(Object).call === Function.prototype.call
<<< true
And as mentioned by @TJ, using a constructor as prototype is rather odd. You should specify an object as the prototype that the created object will inherit from. @TJ already did a pretty good job explaining this part.
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