Copy prototype for inheritance?

前端 未结 1 515
清酒与你
清酒与你 2021-02-05 08:08

I was playing around with JavaScript in particular simulating object oriented programming with classes and whatnot.

I knew about this way of achieving inheritance

<
相关标签:
1条回答
  • 2021-02-05 08:43

    This is almost exactly what Object.create does. The function you've written is a pretty standard "polyfill" for that method.

    This is a really common way of abstracting object creation in a way that more closely reflects "true" prototypal inheritance. Definitely a safe way to do things.

    Oh, and here's a link to the MDN entry for Object.create, if you're interested: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/create/

    You'll notice at the bottom that they actually include the polyfill, which is pretty much identical to your code, save for some safety checks and variable names.

    0 讨论(0)
提交回复
热议问题