Using “Object.create” instead of “new”

后端 未结 15 2424
隐瞒了意图╮
隐瞒了意图╮ 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:51

    TL;DR:

    new Computer() will invoke the constructor function Computer(){} for one time, while Object.create(Computer.prototype) won't.

    All the advantages are based on this point.

    Sidenote about performance: Constructor invoking like new Computer() is heavily optimized by the engine, so it may be even faster than Object.create.

提交回复
热议问题