为什么*不是*“继承”/在JavaScript中从Object扩展? [英] Why *not* "inherit"/extend from Object in JavaScript?

查看:70
本文介绍了为什么*不是*“继承”/在JavaScript中从Object扩展?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,使用JSON表示法声明对象使它们从基础对象继承(或者更确切地说,构建为基础对象):

It is well-known that declaring objects with JSON notation makes them "inherit" from (or, more precisely, to be built like) the base Object:

myobj = {a:1,b:2};

相当于 myobj = Object.create(Object); myobj.a = 1; myobj.b = 2;

而不是:


Object.getPrototypeOf(myobj)

Object.getPrototypeOf(myobj)

打印以下内容:

Object
    __defineGetter__: function __defineGetter__() { [native code] }
    __defineSetter__: function __defineSetter__() { [native code] }
    __lookupGetter__: function __lookupGetter__() { [native code] }
    __lookupSetter__: function __lookupSetter__() { [native code] }
    constructor: function Object() { [native code] }
    hasOwnProperty: function hasOwnProperty() { [native code] }
    isPrototypeOf: function isPrototypeOf() { [native code] }
    propertyIsEnumerable: function propertyIsEnumerable() { [native code] }
    toLocaleString: function toLocaleString() { [native code] }
    toString: function toString() { [native code] }
    valueOf: function valueOf() { [native code] }

然而,一个可以在提供 null 作为参数时调用 Object.create()

However, one can call Object.create() while supplying null as an argument:

myobj2 = Object.create(null);

在这种情况下,将返回空原型:

In this case empty prototype will be returned:

Object
    No Properties

在这里问题是:在哪些情况下,我为什么要关心将原型链分解为原始对象?它有用吗?

And here goes the question: in which cases and why should I care to break the prototype chain to the original Object? Where it can be useful?

更新:已经在下面更正,我的意思是 Object.create(Object.prototype)而不是 Object.create(Object)将返回 Function object(确实 Object() Object prototypees的构造函数。

Update: as already corrected down below, I mean Object.create(Object.prototype) rather than Object.create(Object) that would return Function object (indeed Object() is a constructor function for Object prototypees).

推荐答案

而不是:

myobj = Object.create(Object);

...我想你的意思是等价物:

...I think you mean this is the equivalent:

myobj = Object.create(Object.prototype);

...因为:

Object.getPrototypeOf( {a:1, b:2} ) === Object.prototype;  // true






至于为何使用 null 早期,如果您的对象不需要 Object.prototype 的任何属性,那么在技术上尽早结束链(虽然稍微有点)当有问题的对象上的 不存在 时加速属性查找。


As to why to use null early, if your object has no need for any of the properties of Object.prototype, ending the chain early would technically (though marginally) speed up property lookups when a property does not exist on the object in question.

请注意,我说早,因为链总是以 null 结尾。

Object.getPrototypeOf( Object.prototype );  // null








             obj ----------> proto -------> Object.proto -----> null
         +---------+      +---------+      +-------------+     
         |         |      |         |      |             |
         |  foo:1  |      |  bar:1  |      | toString:fn |      null
         |         |      |         |      |             |
         +---------+      +---------+      +-------------+ 
              ^                ^                  ^               X
              |                |                  |               |
obj.foo ------+                |                  |               |
              ^                |                  |               |
obj.bar-------+----------------+                  |               |
              ^                ^                  |               |
obj.toString--+----------------+------------------+               |
              ^                ^                  ^               |
obj.baz-------+----------------+------------------+---------------+

   ^---property lookups

请注意原型链中的任何地方都不存在 baz 属性。

Notice that the baz property does not exist anywhere in the prototype chain.

因此,它需要按顺序搜索每个对象,直到它最终达到 null ,然后才意识到 baz 在任何地方都不存在。

Because of this, it needs to search each object in sequence until it finally reaches null before it realizes that baz doesn't exist anywhere.

如果你消除 Object.prototype 从链中,它会更快地到达 null

If you eliminate the Object.prototype from the chain, it will get to null a little quicker.

这篇关于为什么*不是*“继承”/在JavaScript中从Object扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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