Using “Object.create” instead of “new”

后端 未结 15 2158
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 06:08

Javascript 1.9.3 / ECMAScript 5 introduces Object.create, which Douglas Crockford amongst others has been advocating for a long time. How do I replace new

15条回答
  •  醉话见心
    2020-11-22 06:57

    Sometimes you cannot create an object with NEW but are still able to invoke the CREATE method.

    For example: if you want to define a Custom Element it must derive from HTMLElement.

    proto = new HTMLElement  //fail :(
    proto = Object.create( HTMLElement.prototype )  //OK :)
    document.registerElement( "custom-element", { prototype: proto } )
    

提交回复
热议问题