Crockford的对象创建技术发生了什么? [英] What is happening in Crockford's object creation technique?

查看:131
本文介绍了Crockford的对象创建技术发生了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只有3行代码,但我无法完全理解这一点:

There are only 3 lines of code, and yet I'm having trouble fully grasping this:

Object.create = function (o) {
    function F() {}
    F.prototype = o;
    return new F();
};
newObject = Object.create(oldObject);

(来自 Prototypal Inheritance


  1. Object.create() 首先创建一个名为 F 的空函数。我认为函数是一种对象。这个 F 对象存储在哪里?在全球范围内我猜。

  1. Object.create() starts out by creating an empty function called F. I'm thinking that a function is a kind of object. Where is this F object being stored? Globally I guess.

接下来我们的 oldObject ,传入 o ,成为函数 F 的原型。函数(即对象) F 现在从我们的 oldObject 继承,因为名称解析将通过它。很好,但我很好奇默认原型是什么对象,对象?这对于函数对象也是如此吗?

Next our oldObject, passed in as o, becomes the prototype of function F. Function (i.e., object) F now "inherits" from our oldObject, in the sense that name resolution will route through it. Good, but I'm curious what the default prototype is for an object, Object? Is that also true for a function-object?

最后, F 被实例化并返回,成为我们的 newObject 。这里严格要求 new 操作吗?不是 F 已经提供了我们需要的东西,或者函数对象和非函数对象之间是否存在重要区别?显然,使用这种技术不可能有一个构造函数。

Finally, F is instantiated and returned, becoming our newObject. Is the new operation strictly necessary here? Doesn't F already provide what we need, or is there a critical difference between function-objects and non-function-objects? Clearly it won't be possible to have a constructor function using this technique.

下一次<$>是否调用了c $ c> Object.create()?全局函数 F 是否被覆盖?当然它不会被重用,因为这会改变以前配置的对象。如果多个线程调用 Object.create()会发生什么,是否存在任何类型的同步以防止 F ?

What happens the next time Object.create() is called? Is global function F overwritten? Surely it is not reused, because that would alter previously configured objects. And what happens if multiple threads call Object.create(), is there any sort of synchronization to prevent race conditions on F?

推荐答案


1)Object.create()以
开头创建一个空函数叫做F.
我认为函数是一个
的对象。这个F对象
存储在哪里?在全球范围内我猜。

1) Object.create() starts out by creating an empty function called F. I'm thinking that a function is a kind of object. Where is this F object being stored? Globally I guess.

不,它存储在 Object.create 函数,每次调用 Object.create 时,将重新创建此函数 F

No, it's stored on the local scope of the Object.create function, each time you invoke Object.create this function F will be recreated.

您甚至可以通过在闭包上存储 F 来创建更具内存效率的实现,并重用它:

You could even create a more memory-efficient implementation, by storing F on a closure, and reuse it:

if (typeof Object.create !== "function") {
  Object.create = (function () {
    function F() {} // created only once
    return function (o) {
      F.prototype = o; // reused on each invocation
      return new F();
    };
  })();
}




2)接下来我们的oldObject,传入as o,
成为函数F的原型。
函数(即对象)F现在
继承来自我们的oldObject,在
意义上,名称解析将路由
通过它。很好,但我很好奇
默认原型是什么
对象,对象?对于
a函数对象也是如此吗?

2) Next our oldObject, passed in as o, becomes the prototype of function F. Function (i.e., object) F now "inherits" from our oldObject, in the sense that name resolution will route through it. Good, but I'm curious what the default prototype is for an object, Object? Is that also true for a function-object?

所有对象都有一个内部属性来构建原型链,这个属性被称为 [[Prototype]] ,它是一个内部属性,虽然有些实现允许你使用 obj .__ proto__访问它,比如mozilla。 property。

All objects have an internal property that builds the prototype chain, this property is known as [[Prototype]], it's an internal property, although some implementations let you access to it, like mozilla, with the obj.__proto__ property.

创建新对象时的默认 [[Prototype]] ,即 var obj = {}; Object.prototype

The default [[Prototype]] when you create a new object, i.e. var obj = {}; is Object.prototype.

所有函数都有 prototype 属性,当函数用作构造函数,使用 new 运算符调用。

All functions have a prototype property, this property is used when a function is used as a Constructor, invoked with the new operator.

在幕后创建的新对象实例,此对象 [[Prototype]] 设置为其构造函数的原型 prope rty。

A new object instance it's created behind the scenes, and this object [[Prototype]] is set to its Constructor's prototype property.


3)最后,F被实例化并返回
,成为我们的newObject。
的新操作是否必须在这里?b $ b? F是否已经提供了我们需要的
,或者函数对象
和非函数对象之间是否存在关键的
差异?很明显,使用这个
技术,
将不可能有
构造函数。

3) Finally, F is instantiated and returned, becoming our newObject. Is the "new" operation strictly necessary here? Doesn't F already provide what we need, or is there a critical difference between function-objects and non-function-objects? Clearly it won't be possible to have a constructor function using this technique.

是的, new 运算符在此方法中至关重要。

Yes, the new operator is essential in this method.

new 运算符是设置对象的 [[Prototype]] 内部属性的唯一标准方法,如果你对它是如何工作很好奇,你可以给出查看 [[Construct]] 内部操作。

The new operator is the only standard way to set the [[Prototype]] internal property of an object, if you are curious about how it works, you can give a look to the [[Construct]] internal operation.


下次调用
Object.create()时会发生什么?全局
函数F是否被覆盖?肯定是
没有被重用,因为这会改变以前配置的
b。并且
如果多个线程调用
Object.create()会发生什么,是否有任何类型的
同步以阻止F上的种族
条件?

What happens the next time Object.create() is called? Is global function F overwritten? Surely it is not reused, because that would alter previously configured objects. And what happens if multiple threads call Object.create(), is there any sort of synchronization to prevent race conditions on F?

下次调用 Object.create 时,新的本地 F 函数仅在方法调用的范围内实例化,你不应该担心竞争条件

The next time Object.create is invoked, a new local F function is instantiated only within the scope of the method call, you shouldn't worry about race conditions.

注意这个实现几乎不符合 Object.create rel =noreferrer> ECMAScript第5版规范,在该方法中,您可以传递属性描述符来初始化对象。

Note that this implementation hardly conforms the Object.create described in the ECMAScript 5th Edition Specification, in that method, you could pass a property descriptor to initialize the object.

所有浏览器供应商都在实施它(已经在Firefox 3.7 alphas,最新的Wekit Nightly Builds和Chrome 5 Beta上提供),所以我建议你至少在覆盖之前检查本机实现是否存在。

All browser vendors are implementing it (already available on Firefox 3.7 alphas, latest Wekit Nightly Builds and Chrome 5 Beta), so I would recommend you at least to check if a native implementation exist before overriding it.

这篇关于Crockford的对象创建技术发生了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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