search

Home  >  Q&A  >  body text

javascript - What is the difference between Object.create(null) and {}

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

扔个三星炸死你扔个三星炸死你2828 days ago772

reply all(4)I'll reply

  • 滿天的星座

    滿天的星座2017-06-30 09:59:55

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-06-30 09:59:55

    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

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-06-30 09:59:55

    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.

    reply
    0
  • 高洛峰

    高洛峰2017-06-30 09:59:55

    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

    reply
    0
  • Cancelreply