Ai
1 Star 0 Fork 0

DragonHot/js-code

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ex1.js 764 Bytes
一键复制 编辑 原始数据 按行查看 历史
DragonHot 提交于 2022-01-10 23:15 +08:00 . 继承
// es6继承
class Point {
x;
y;
constructor(x, y) {
this.x = x;
this.y = y;
}
print() {
console.log(`Point:(${this.x},${this.y})`);
}
}
class ColorPoint extends Point {
color;
constructor(x, y, color) {
// 必须调用父类构造函数
super(x, y);
this.color = color;
}
// 覆盖父类方法
print() {
console.log(`ColorPoint:(${this.x},${this.y}) ${this.color}`);
}
}
const p = new Point(1, 2);
p.print(); // Point:(1,2)
const cp = new ColorPoint(3, 4, "red");
cp.print(); // ColorPoint:(3,4) red
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

搜索帮助