Understanding the difference between Object.create() and new SomeFunction()

2022-08-29 23:20:20

I recently stumbled upon the method in JavaScript, and am trying to deduce how it is different from creating a new instance of an object with , and when you would want to use one over the other.Object.create()new SomeFunction()

Consider the following example:

var test = {
  val: 1,
  func: function() {
    return this.val;
  }
};
var testA = Object.create(test);

testA.val = 2;
console.log(test.func()); // 1
console.log(testA.func()); // 2

console.log('other test');
var otherTest = function() {
  this.val = 1;
  this.func = function() {
    return this.val;
  };
};

var otherTestA = new otherTest();
var otherTestB = new otherTest();
otherTestB.val = 2;
console.log(otherTestA.val); // 1 
console.log(otherTestB.val); // 2

console.log(otherTestA.func()); // 1
console.log(otherTestB.func()); // 2

Notice that the same behaviour is observed in both cases. It seems to me that the primary differences between these two scenarios are:

  • The object used in actually forms the prototype of the new object, whereas in the from the declared properties/functions do not form the prototype. Object.create()new Function()
  • You cannot create closures with the syntax as you would with the functional syntax. This is logical given the lexical (vs block) type scope of JavaScript.Object.create()

Are the above statements correct? And am I missing something? When would you use one over the other?

EDIT: link to jsfiddle version of above code sample: http://jsfiddle.net/rZfYL/


答案 1

Very simply said, is with additionally running the function. (And giving the the chance to the actual object that should be the result of the expression instead of .)new XObject.create(X.prototype)constructorconstructorreturnthis

That’s it. :)

The rest of the answers are just confusing, because apparently nobody else reads the definition of new either. ;)


答案 2

The object used in Object.create actually forms the prototype of the new object, where as in the new Function() form the declared properties/functions do not form the prototype.

Yes, builds an object that inherits directly from the one passed as its first argument.Object.create

With constructor functions, the newly created object inherits from the constructor's prototype, e.g.:

var o = new SomeConstructor();

In the above example, inherits directly from .oSomeConstructor.prototype

There's a difference here, with you can create an object that doesn't inherit from anything, , on the other hand, if you set the newly created object will inherit from .Object.createObject.create(null);SomeConstructor.prototype = null;Object.prototype

You cannot create closures with the Object.create syntax as you would with the functional syntax. This is logical given the lexical (vs block) type scope of JavaScript.

Well, you can create closures, e.g. using property descriptors argument:

var o = Object.create({inherited: 1}, {
  foo: {
    get: (function () { // a closure
      var closured = 'foo';
      return function () {
        return closured+'bar';
      };
    })()
  }
});

o.foo; // "foobar"

Note that I'm talking about the ECMAScript 5th Edition Object.create method, not the Crockford's shim.

The method is starting to be natively implemented on latest browsers, check this compatibility table.