简体   繁体   English

关于javascript练习的困惑

[英]confusion about javascript exercise

I just got Javascript: The Good Parts by Douglas Crockford and I'm having some difficulty understanding one of his examples regarding prototypes. 我刚刚得到了道格拉斯·克罗克福德的Javascript:The Good Parts,我很难理解他关于原型的一个例子。 The code in the book reads as follows: 书中的代码如下:

if (typeof Object.create !== "function") {
    Object.create = function(o) {
        var F = function () {}
        F.prototype = o;
        return new F;
    };
}

I'm assuming that this code is used to target a function's prototype. 我假设这段代码用于定位函数的原型。 But why use such a complex approach? 但为什么要使用这种复杂的方法呢? Why not just use variable .prototype? 为什么不使用variable .prototype? Crockford is the leading expert on Javascript so I'm sure there is a good reason for using this model. Crockford是Javascript领域的领先专家,所以我确信有充分的理由使用这个模型。 Can anyone help me understand it better? 谁能帮助我更好地理解它? Any help would be appreciated. 任何帮助,将不胜感激。

In ECMAScript 3, the new operator was the only standard way to set the [[Prototype]] internal property of an object, in this case, Crockford is just using a temporary constructor function F for that purpose. 在ECMAScript 3中, new运算符是设置对象[[Prototype]] 内部属性的唯一标准方法,在这种情况下,Crockford只是为此目的使用临时构造函数 F

The o argument of that method, is set as the prototype property of the temporary constructor, and by invoking new F(); 该方法的o参数被设置为临时构造函数的prototype属性,并通过调用new F(); it builds a new empty object that inherits from F.prototype (see this question for more details about how new works). 它构建了一个继承自F.prototype的新空对象(有关new工作原理的更多详细信息,请参阅此问题 )。

For example: 例如:

var a = { a: 1 };
var b = Object.create(a); // b inherits from a
b.a; // 1

In the above example, we can say that the b 's internal [[Prototype]] property points to a . 在上面的例子中,我们可以说b的内部[[Prototype]]属性指向a

Object.getPrototypeOf(b) === a; // true

In other words, b inherits from a . 换句话说, b继承自a

With the same example, we could use an empty constructor, eg: 使用相同的示例,我们可以使用空构造函数,例如:

 function F(){}
 F.prototype = a;

 var b = new F(); // b again inherits from a (F.prototype)

Remember also that the prototype property of functions is different than the [[Prototype]] property that all objects have, the prototype property of functions is used when they are called with the new operator, to build a new object that inherits from that property. 还要记住,函数的prototype属性与所有对象都具有的[[Prototype]]属性不同,函数的prototype属性在使用new运算符调用时用于构建从该属性继承的新对象。

Also, be aware that now, the ECMAScript 5 Standard is being implemented, and this shim, doesn't conform 100% with the spec, in fact, there are some features of the standard Object.create method that cannot be emulated on ES3 . 此外,请注意,现在,ECMAScript 5标准正在实施,而且这个垫片不符合规范100%,事实上,标准的Object.create方法有一些功能无法在ES3上模拟

See also: 也可以看看:

var bar = Object.create(foo)

vs.

var bar = new Object()

the first bar has foo as its prototype; 第一个barfoo为原型; the second has Object as its prototype. 第二个是Object作为原型。

This code is for older JavaScript implementations that do not support Object.create , which is specified in the ECMAScript 5 standard released in November 2009. 此代码适用于不支持Object.create旧JavaScript实现,后者在2009年11月发布的ECMAScript 5标准中指定。

Many people say the preferred way to create an object is to specify a prototype for it at the time of creation. 许多人说创建对象的首选方法是在创建时为其指定原型。 This can be called differential inheritance or prototypal inheritance . 这可以称为differential inheritanceprototypal inheritance In fact, that is what Object.create does: 实际上,这就是Object.create作用:

var protoCircle = {x: 0, y: 0, radius: 1, color:"black"};
var myCircle = Object.create(protoCircle);
myCircle.x = 3;
myCircle.color = "green";

This make a green circle of radius 1 centered at (3,0). 这使得半径为1的绿色圆圈以(3,0)为中心。

The reason the code is so complex that before Object.create was added to JavaScript, the only way to set an object's prototype was to create it with the new operator . 代码如此复杂以至于在将Object.create添加到JavaScript之前, 设置对象原型唯一方法是使用new运算符创建它 Objects created with new got, as their prototype, the value of the constructor's prototype property. 使用new创建的对象作为其原型获取构造函数的prototype属性的值。 It's definitely confusing, but the prototype property is NOT the prototype of an object. 这绝对令人困惑,但prototype属性不是对象的原型。 Given a function object f , f.prototype is the object that will be assigned as the prototype of all objects constructed through new f() . 给定一个函数对象ff.prototype是将被指定为通过new f()构造的所有对象的原型的对象。 An object's real prototype is called [[prototype]] or __proto__ but you cannot access these in standard ECMAScript. 对象的真实原型称为[[prototype]]__proto__但您无法在标准ECMAScript中访问它们。 Confusing, eh? 令人困惑,是吗?

As a side note. 作为旁注。 the ES5 specification has a more enhanced Object.prototype specification than the one Crockford defined. ES5规范具有比Crockford定义的更强的Object.prototype规范。 It takes a second object for configuring properties of the object being defined. 它需要第二个对象来配置正在定义的对象的属性。

给定传递的对象作为原型,此create方法将实例化一个新对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM