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.
Felix kling
source share