代码拉取完成,页面将自动刷新
function Point(x, y) {
this.x = x;
this.y = y;
this.arr = [x, y];
}
Point.prototype.print = function () {
console.log(`Point:(${this.x},${this.y})`);
};
function ColorPoint(color) {
this.color = color;
}
// 将子类的prototype改成父类的实例
ColorPoint.prototype = new Point(1, 2);
ColorPoint.prototype.print = function () {
console.log(`ColorPoint:(${this.x},${this.y}) ${this.color} [${this.arr}]`);
};
const cp = new ColorPoint("red");
cp.print(); // ColorPoint:(1,2) red [1,2]
const cp2 = new ColorPoint("blue");
cp2.x = 233; // 这种操作只是在cp2增加了一个x属性,不会影响cp的x
cp2.arr[0] = 10086; // 这种操作修改了父类的那个实例的arr的值,会影响到cp
cp2.print(); // ColorPoint:(233,2) blue [10086,2]
cp.print(); // ColorPoint:(1,2) red [10086,2]
console.log(cp instanceof Point); // ture
console.log(cp instanceof ColorPoint); // ture
console.log(Point.prototype.isPrototypeOf(cp)); // ture
console.log(ColorPoint.prototype.isPrototypeOf(cp)); // ture
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。