Object.create(null) and {} What is the difference? vuexThe source code determines that the objects are both Object.create(null) Why not just use {}
Object.create(null)
{}
vuex
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
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.
Object.create()
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
Object.create(null)
does not inherit any prototype method, which means that its prototype chain does not have a higher level.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.Detailed explanation