理解Object.create()和new SomeFunction()之间的区别 [英] Understanding the difference between Object.create() and new SomeFunction()

查看:133
本文介绍了理解Object.create()和new SomeFunction()之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近偶然发现了JavaScript中的 Object.create()方法,并试图推断它与使用<$创建对象的新实例的不同之处c $ c> new SomeFunction(),以及何时想要使用其中一个。

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

请考虑以下示例:

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:


  • Object.create中使用的对象( )实际上形成了新对象的原型,而在声明的属性/函数的 new Function()中则不构成原型。

  • 您不能像使用函数语法一样使用 Object.create()语法创建闭包。鉴于JavaScript的词法(vs块)类型,这是合乎逻辑的。

  • The object used in Object.create() actually forms the prototype of the new object, whereas in the new Function() from the declared properties/functions do not form the 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.

以上陈述是否正确?我错过了什么吗?你什么时候用一个而不是另一个?

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

编辑:链接到上面代码示例的jsfiddle版本: http: //jsfiddle.net/rZfYL/

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

推荐答案


Object中使用的对象。 create实际上形成了新对象的原型,而在新的Function()形式中,声明的属性/函数不构成原型。

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.

是, Object.create 构建一个直接从作为第一个参数传递的对象继承的对象。

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

使用构造函数,新创建的对象继承自构造函数的原型,例如:

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

var o = new SomeConstructor();

在上面的例子中, o 直接继承来自 SomeConstructor.prototype

In the above example, o inherits directly from SomeConstructor.prototype.

这里有一个区别, Object.create 你可以创建一个不从任何东西继承的对象, Object.create(null); ,另一方面,如果你设置 SomeConstructor.prototype = null; 新创建的对象将继承自 Object.prototype

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


您不能像使用函数语法一样使用Object.create语法创建闭包。鉴于JavaScript的词法(vs块)类型范围,这是合乎逻辑的。

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"

请注意,我在谈论ECMAScript第5版 Object.create 方法,而不是Crockford的垫片。

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.

这篇关于理解Object.create()和new SomeFunction()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆