Is there a reason to use Object.create () or a new one in JavaScript?

I have used the new keyword in JavaScript so far. I read about Object.create and I am wondering if I should use it instead. I don’t quite understand that I often need to run the build code, so I don’t see how Object.create will work at all, since it does not run any functions to run.

Can someone tell me when to use Object.create instead of new ?

+7
source share
4 answers

For now, if you want to create an object, you can only use literals:

 var obj = {}; 

or constructor Object .

 var obj = Object(); 

But none of these methods allows you to specify the prototype of the created object.

This is what you can do with Object.create now. It allows you to create a new object and sets the first argument as a prototype of the new object. In addition, it allows you to set the properties of the new object provided as the second argument.

This is similar to doing something like this (without the second argument):

 function create(proto) { var Constr = function(){}; Constr.prototype = proto; return new Constr(); } 

So, if you use a similar construct, this is when you want to use Object.create .

This is not a replacement for new . This is more like creating separate objects that are easier to inherit from another object.

Example:

I have an a object:

 var a = { someFunction: function() {} }; 

and I want b extend this object. Then you can use Object.create :

 b = Object.create(a); b.someOtherFunction = function(){}; 

Whenever you have a constructor function, but you only instantiate one object, you can replace this with Object.create .

There is a general rule. It very much depends on what the constructor function does and how you inherit other objects, etc.

+11
source

As already mentioned, Object.create() usually used when you need an easy way to set up a prototype of a new object. Other answers do not mention that constructor functions (requiring new ones) are not all that are different from any other function.

In fact, any function can return an object, and factory functions are often found in JavaScript (such as constructors, but they do not require new or use this to refer to a new object), factory functions often use Object.create() to set the prototype of a new object.

 var barPrototype = { open: function open() { /* ... */ }, close: function close() { /* ... */ }, }; function createBar() { return Object.create(barPrototype); } var bar = createBar(); 
+3
source

It's super late to this topic .. but I think the important difference that needs to be made is that although constructors are just functions, the new operator calls the function and captures the resulting object, which can be useful in a dynamic environment. It also allows you to reference properties and methods at runtime, which may or may not be useful depending on the situation. If you are more concerned about having a prototype set, but the object itself is more static than not, Object.create would be a better option, as it is cleaner and not messed with the proto chain in unexpected ways, like the new statement does.

some simple examples.

 var Foo = function(element) { this.elem = element; $(this.elem).css('color', 'blue'); // using the new operator here gives access to this.elem // immediately after that declaration is made, thus providing // a comfortable dynamic system to work with } var bar = new Foo(someElem); 

how was that possible.

 var foo = { elem : element, // assume elem exists $(this.elem).css('color', 'blue')// in the lexical scope } var bar = Object.create(foo); // This.elem does not exist until the object is returned now // and the .css method call will not execute 

It should be a little clearer why you would like to use one above the other as another simple breakdown ...

Use "New" when you need to have a dynamic object and less about the prototype chain.

Use Object.create when you need less to be dynamic and more to have an explicit prototype chain. I will also note that objects created using Object.create can be dynamically built using the init method, which you can build to assign properties based on your needs and simply call it on the newly returned object.

Each approach has its own problems and falls, however, in my opinion, their use cases are quite different. However, you will most likely find that you are using Object.create, as most situations will require a more dynamic situation and more need for inheritance.

+2
source

The exact source code for the Object.create () function:

 function Object.Create(proto, propertiesObject) { var obj = {}; Object.setPrototypeOf(obj, proto); if(propertiesObject) { Object.defineProperties(obj, propertiesObject); } return obj; } 
0
source

All Articles