On understanding correlation of bind and inheritance

后端 未结 1 1396
隐瞒了意图╮
隐瞒了意图╮ 2021-01-14 08:57

Today I was reading the MDN documentation on Function.prototype.bind(). Under the section Bound functions used as constructors there is an example that I cannot quite unders

相关标签:
1条回答
  • 2021-01-14 09:03

    But how in the world can new Point(17,42) be an instance of YAxisPoint?

    Because instanceof works special with bound functions (those created from .bind() calls). Usually it would check whether the object inherits from the constructors .prototype, but bound functions don't have a .prototype. Instead, when you use instanceof on a bound function, it checks whether the object is an instance of the target function (that bind() was called upon). So

    … instanceof YAxisPoint
    

    is exactly equivalent to

    … instanceof Point
    

    You can check this in the specs (ES5, ES6).

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