Ai
1 Star 0 Fork 0

DragonHot/js-code

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ex2.js 1022 Bytes
一键复制 编辑 原始数据 按行查看 历史
DragonHot 提交于 2022-01-10 23:15 +08:00 . 继承
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
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/dragonhot/js-code.git
git@gitee.com:dragonhot/js-code.git
dragonhot
js-code
js-code
master

搜索帮助