What it the significance of the Javascript constructor property?

前端 未结 6 1061
一生所求
一生所求 2020-11-22 04:23

Trying to bend by head around Javascript\'s take on OO...and, like many others, running into confusion about the constructor property. In particular, the signif

6条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 04:52

    September 2020 Update

    The answer below is from the days of ECMAScript 3 and the first sentence is no longer true because since ECMAScript 6, the constructor property is used in a few places. However, I think the overall gist still applies. Thanks to T. J. Crowder for pointing that out in the comments, and please read his answer for a fuller picture of the current situation.

    Original answer

    The constructor property makes absolutely no practical difference to anything internally. It's only any use if your code explicitly uses it. For example, you may decide you need each of your objects to have a reference to the actual constructor function that created it; if so, you'll need to set the constructor property explicitly when you set up inheritance by assigning an object to a constructor function's prototype property, as in your example.

提交回复
热议问题