了解Crockford的Object.create垫片 [英] Understanding Crockford's Object.create shim
问题描述
我一直在阅读Crockford垫片,以防止覆盖原型,并了解它有时不是最终/全部解决方案。我也明白, ES5 Shim 可能是一个可行的替代方案。我还阅读了这篇文章,它提供了一个更强大,更安全的替代方案。
I've been reading up on the Crockford shim for preventing the overwriting of prototypes, and understand that it's not the end-all/be-all solution at times. I also understand that ES5 Shim may be a viable alternative to this. I also read this post which provides a more robust, secure alternative.
但是,我想知道他的 Object.create
垫片是什么说然后干嘛。有人可以告诉我,如果我的解释评论是正确的吗?
Still, I'd like to know what his Object.create
shim is "saying" and then "doing." Can someone please tell me if my explanation comments are right?
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'对象的原型不会被覆盖'another_stooge'。无需使用'构造函数'重置'stooge'原型。
This way, the prototype of the 'stooge' object can't be overwritten when we augment stuff to 'another_stooge'. No need to reset the 'stooge' prototype using 'constructor'.
提前致谢,
-k
推荐答案
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
在上面的例子中,我们创建了一个新对象 newObject
使用 create
方法,它是我们添加的 Object
对象的成员函数在我们(Crockford)示例中的 Object
对象中。基本上它的作用是, create
方法声明一个函数 F
,一个空对象每个函数都是javascript中的第一类对象
然后我们继承了 o
的原型(在这种情况下 o
也是作为create method参数传递的对象 oldObject
,最后我们返回了新对象(F的一个实例) 将新的F();
返回给变量 newObject
,所以现在 newObject
是继承 oldObject
的对象。现在,如果您编写 console.log(newObject.prop);
,那么它将输出 Property_one
,因为我们的 newObject
object继承了 oldObject
,这就是为什么我们得到了 prop $ c的值$ c> as
Property_one
。这称为原型继承。
In the above example we've created a new object newObject
using create
method which is a member function of Object
object that we've added in the Object
object earlier in our (Crockford's) example. So basically what it does is that, the create
method declares a function F
, an empty object every function is a first class object in javascript
and then we've inherited the prototype of o
(in that case o
is also an object oldObject
passed as the parameter of create method) and finally we've returned the new object (an instance of F) using return new F();
to the variable newObject
, so now newObject
is an object that inherited the oldObject
. Now if you write console.log(newObject.prop);
then it'll output Property_one
because our newObject
object has inherited the oldObject
and that's why we've got the value of prop
as Property_one
. this is known as prototypical inheritance.
您必须传递一个对象作为参数 create
method
You must pass an object as the parameter of create
method
这篇关于了解Crockford的Object.create垫片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!