今日はFunction.prototype.bind()のMDNドキュメントを読んでいました。セクションBound functions used as constructorsの下に、私がかなり理解できない例があります。バインドと継承の相関の理解について
私はaxisPoint
がのインスタンスである理由私ははっきりと見ることができるのNode.js(v.4.4.5)とGoogle Chromeの(v58.0.3029.81)の両方で
function Point(x, y) {
this.x = x;
this.y = y;
}
Point.prototype.toString = function() {
return this.x + ',' + this.y;
};
var p = new Point(1, 2);
p.toString(); // '1,2'
var emptyObj = {};
var YAxisPoint = Point.bind(emptyObj, 1/*x*/);
var axisPoint = new YAxisPoint(5);
console.log(axisPoint.toString()); // '1,5'
console.log(axisPoint instanceof Point); // true
console.log(axisPoint instanceof YAxisPoint); // true
console.log(new Point(17, 42) instanceof YAxisPoint); // true
を以下のコードを実行しましたPoint
とYAxisPoint
の両方です。しかし、どのように世界でnew Point(17,42)
YAxisPoint
のインスタンスになることができますか?