Object.create(Object.prototype),Object.create(Object)和Object.create(null)之间的区别 [英] difference between Object.create(Object.prototype) , Object.create(Object) and Object.create(null)
问题描述
我应该为其他人继承的第一个父对象传递哪个参数,哪个更有效
Which parameter should i pass for the first parent object from which others will inherit and which one is more efficient
Object.create(Object.prototype)
Object.create(Object)
Object.create(null)
Object.create(null)
返回一个空对象
Object.create(Object)
返回一个函数为什么????(我检查了我的日志,它说的是函数...我使用了console.dir())
Object.create(Object)
returns a function why????( I checked my log and it says function...i used console.dir() )
Object.create(Object)
返回非空对象
整个事情是如何工作的...我更习惯于Classname .prototype的事情:(
How does this whole thing work ... I m more used to the Classname .prototype thing :(
无法理解这里发生了什么
Can't understand what is going on here
推荐答案
前言:JavaScript使用原型继承,这意味着对象可以拥有(通常具有)原型在它后面,这是另一个对象。如果你试图从一个目标中获取一个属性的值它没有,JavaScript引擎查找对象的原型(及其原型,等等)来查找它。
Preface: JavaScript uses prototypical inheritance, which means that an object can have (usually does have) a prototype behind it, which is another object. If you try to get the value of a property from an object that it doesn't have, the JavaScript engine looks to the object's prototype (and its prototype, and so on) to find it.
Object.create
创建对象。你给出的第一个参数 Object.create
是用作它创建的对象原型的对象。所以:
Object.create
creates objects. The first argument you give Object.create
is the object to use as the prototype of the object it creates. So:
// Create an object with a property 'foo'
var a = {
foo: 42
};
// Create a blank object using `a` as its prototype
var b = Object.create(a);
// Give `b` a property of its own
b.bar = "hi";
这给了我们内存:
+---------------+ +-------------------+
| [[Prototype]] |----->| (the standard |
a----------------------+-->| foo: 42 | | object prototype) |
| +---------------+ +-------------------+
|
+---------------+ |
b-->| [[Prototype]] |--+
| bar: "hi" |
+---------------+
证明 b
使用 a
:
console.log(b.foo); // 42
a.foo = 67;
console.log(b.foo); // 67
解决部分变化:
var o = Object.create(Object.prototype);
这是毫无意义的,只需使用 var o = {};
,它做同样的事情(创建一个新的空白对象,其原型是 Object.prototype
)。
That's pointless, just use var o = {};
, it does the same thing (creates a new blank object whose prototype is Object.prototype
).
var o = Object.create(Object);
创建一个新的空白对象 o
其原型是对象
函数。它不会创建一个函数,只是一个具有函数作为其原型的非函数对象。这可能很奇怪,可能不是你想要的。
Creates a new blank object o
whose prototype is the Object
function. It doesn't create a function, just a non-function object that has a function as its prototype. This would be quite odd and probably isn't what you want.
var o = Object.create(null);
创建一个新的空白对象 o
其原型是 null
。由于它的原型是 null
,它没有通常的 Object.prototype
的东西,比如 toString
和 valueOf
和 hasOwnProperty
。这有点不寻常,虽然有一些用例,例如当你使用一个对象作为字典/地图而不想要那些属性名称的误报时。 (在ES2015 [aka ES6]中,另一种选择是使用 Map
。)
Creates a new blank object o
whose prototype is null
. Since its prototype is null
, it doesn't have the usual Object.prototype
stuff, like toString
and valueOf
and hasOwnProperty
. That's a bit unusual, although there are use cases for it, such as when you're using an object as a dictionary/map and don't want false positives for those property names. (In ES2015 [aka ES6] another option is to use Map
instead.)
正如thg435在下面的评论中指出的那样,关于JavaScript的一个令人困惑的事情是,对象的原型与 prototype
属性完全不同你看到的功能。如果 prototype
属性有一个不同的名称可能会更好(虽然我无法想象没有大量笨重的名字)。
As thg435 points out in a comment below, one of the confusing things about JavaScript is that the prototype of an object is a completely different thing from the prototype
property you see on functions. It would probably be better if the prototype
property had had a different name (although I can't imagine what name it would be without being massively clunky).
一个对象(我们称之为 o
)有一个继承属性的原型对象。函数的 prototype
属性上的对象不一定是任何对象的原型。相反,它是将被指定为使用该函数通过 new
创建的任何对象的原型的对象。
An object (let's call it o
) has a prototype object it inherits properties from. The object on the prototype
property of functions is not necessarily the prototype of any object at all. Instead, it's the object that will be assigned as the prototype of any object created via new
using that function.
这里有例子帮助。
function Foo() {
}
该函数 Foo
,属性 Foo.prototype
引用一个对象。那个对象还没有用作任何东西的原型。它只是在 Foo
对象实例上分配给名为 prototype
的属性的对象。
That function, Foo
, has a property Foo.prototype
that refers to an object. That object is not, yet, used as the prototype of anything. It's just an object assigned to a property called prototype
on the Foo
object instance.
var f = new Foo();
现在该对象被用作原型,特别是它的原型由新Foo
调用创建的 f
对象。
Now that object is used as a prototype, specifically it's the prototype of the f
object created by the new Foo
call.
忽略一些细节,这行代码:
Ignoring a couple of details, this line of code:
var f = new Foo();
...基本上是这样的:
...basically does this:
// Create a blank object, giving it `Foo.prototype` as its prototype
var f = Object.create(Foo.prototype);
// Call` Foo` using that new object as `this`
Foo.call(f);
正如我所说,这留下了一些细节,但希望它有助于说清楚 prototype
函数的属性是...
As I say, that leaves out a couple of details, but hopefully it helps make it clear what the prototype
property of functions is for...
这篇关于Object.create(Object.prototype),Object.create(Object)和Object.create(null)之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!