Javascript 理解Crockford';创建垫片
我一直在阅读Crockford垫片,以防止覆盖原型,并理解它有时不是最终解决方案。我也明白这可能是一个可行的替代方案。我也读书 不过,我还是想知道他的Javascript 理解Crockford';创建垫片,javascript,prototype,object-create,Javascript,Prototype,Object Create,我一直在阅读Crockford垫片,以防止覆盖原型,并理解它有时不是最终解决方案。我也明白这可能是一个可行的替代方案。我也读书 不过,我还是想知道他的对象。createshim是“说”还是“做”的。有人能告诉我我的解释意见是否正确吗 if (typeof Object.create === 'undefined') { //If the browser doesn't support Object.create Object.create = function (o) { //Obj
对象。create
shim是“说”还是“做”的。有人能告诉我我的解释意见是否正确吗
if (typeof Object.create === 'undefined') {
//If the browser doesn't support Object.create
Object.create = function (o) {
//Object.create equals an anonymous function that accepts one parameter, 'o'.
function F() {};
//Create a new function called 'F' which is just an empty object.
F.prototype = o;
//the prototype of the 'F' function should point to the
//parameter of the anonymous function.
return new F();
//create a new constructor function based off of the 'F' function.
};
}
//Then, based off of the 'Lost' example in the Crockford book...
var another_stooge = Object.create(stooge);
//'another_stooge' prototypes off of 'stooge' using new school Object.create.
//But if the browser doesn't support Object.create,
//'another_stooge' prototypes off of 'stooge' using the old school method.
这样,当我们将内容扩充为“另一个”时,“stooge”对象的原型就不会被覆盖。无需使用“构造函数”重置“stooge”原型
提前感谢,
-k它所做的只是创建一个新的对象构造函数,即F,然后将传递的对象分配给构造函数原型属性,以便使用F构造函数创建的新对象继承这些方法 然后使用构造函数返回一个新初始化的对象(new F()=>F.prototype) 但是crockford one无法正确地重新分配构造函数,因为新的对象构造函数通常应该与其继承的对象构造函数相同。在您的评论中:
> //Object.create equals an anonymous function that accepts one parameter, 'o'.
更好的说法是将函数分配给对象
的创建
属性。所有函数都可以被视为匿名函数,只是有些函数被指定给命名属性或变量,而有些函数则不是
> //Create a new function called 'F' which is just an empty object.
声明一个函数。是的,它也是一个物体
> F.prototype = o;
> //the prototype of the 'F' function should point to the
> //parameter of the anonymous function.
对o
的引用被分配给F。prototype
写起来稍微短一点
> //create a new constructor function based off of the 'F' function.
否,应为“返回F的实例”。因此,返回的对象有一个内部的[[Prototype]]
,它引用传递给函数的对象。麻烦的是,必须创建一个无用的F函数来执行该技巧,并且返回的对象的构造函数将没有有用的值,因为它引用了空的F
并不是说构造函数属性非常可靠,或者通常特别有用
这样,“stooge”对象的原型就不会被覆盖
当我们把东西增加到“另一个”的时候。无需重新设置
使用“构造函数”的“stooge”原型
这是一种奇怪的说法*另一个_stooge*拥有stooge
,因为它是私有的[[Prototype]]]
,它不是从stooge.Prototype继承的,而是从stooge.[Prototype]
如果您想从stooge.prototype
继承另一个\u stooge
,请使用Object.create(stooge.prototype)
或Object.create(new stooge())
,前者可能更合适
我希望这一切都有意义
if (typeof Object.create !== 'function') {
Object.create = function (o) {
function F() {}
F.prototype = o;
return new F();
};
}
var oldObject={prop:'Property_one' }; // An object
var newObject = Object.create(oldObject); // Another object
在上面的示例中,我们使用create
方法创建了一个新对象newObject
,该方法是object
对象的成员函数,我们在前面的(Crockford的)示例中添加了object
对象。所以基本上它所做的就是,create
方法声明了一个函数F
,一个空对象,每个函数都是javascript
中的一类对象,然后我们继承了o
的原型(在这种情况下,o
也是作为create方法的参数传递的对象oldObject
),最后我们使用returnnewf()返回了新对象(F的一个实例);
到变量newObject
,因此现在newObject
是继承oldObject
的对象。如果现在写入console.log(newObject.prop)
然后它将输出Property\u one
,因为我们的newObject
对象继承了oldObject
,这就是为什么我们将Property
的值设为Property\u one
。这被称为原型继承
必须将对象作为
创建
方法的参数传递这里有两个技巧:
- F是一个构造函数
- F不是从o继承的
- “new F”(返回的对象)是从o继承的
对象而不是F
会使生成的对象看起来更接近对象。create()
将创建的对象
if (typeof Object.create !== 'function') {
Object.create = function (o) {
function Object() {}
Object.prototype = o;
return new Object();
};
}
嗨,gillesc。感谢您的回复!因此,当使用此选项时,是否意味着需要重置构造函数的原型?类似于:stooge.prototype.constructor=stooge;再次感谢!嗨,RobG。感谢您的回复!我必须重新阅读以完全理解语法,但我肯定比一小时前知道的更多。再次感谢!