javascript - What is the difference between Object.create(null) and {}
扔个三星炸死你
扔个三星炸死你 2017-06-30 09:57:55
0
4
689

Object.create(null) and {} What is the difference? vuexThe source code determines that the objects are both Object.create(null) Why not just use {}

扔个三星炸死你
扔个三星炸死你

reply all(4)
滿天的星座

我想大声告诉你

Object.create(null) does not inherit any prototype method, which means that its prototype chain does not have a higher level.

console.log(Object.create({}).toString);   // function toString() { [native code] }
console.log(Object.create(null).toString); // undefined
过去多啦不再A梦

Object.create() This method is used for inheritance. I remember that it should be called functional inheritance.
In js, null does not have any attributes or methods.
You inherited a null, so there is nothing in it.

小葫芦

The

Object.create() method creates a new object using the specified prototype object and its properties.

var c=Object.create(null), b= {}, a=Object.create(Object.prototype);
console.log(c, b, a);

Object.create(proto, [ propertiesObject ])
proto
一个对象,应该是新创建的对象的原型。
propertiesObject
可选。该参数对象是一组属性与值,该对象的属性名称将是新创建的对象的属性名称,值是属性描述符(这些属性描述符的结构与Object.defineProperties()的第二个参数一样)。注意:该参数对象不能是 undefined,另外只有该对象中自身拥有的可枚举的属性才有效,也就是说该对象的原型链上属性是无效的。
抛出异常

如果 proto 参数不是 null 或一个对象值,则抛出一个 TypeError 异常。

Detailed explanation

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template