原型链

2019-11-20  本文已影响0人  稻草人_9ac7
////////////原来的js创建构造方法
function Cat() { //构造器
    this.age = 20;
    this.name = "李白"

}
Cat.prototype.say = function() {
    console.log("name", this.name)
}
let cat = new Cat()
cat.say() //name 李白

//////////////es6创建构造方法
class Dog {
    constructor() { //构造器
        this.name = "Dog";
        this.color = "白色";
    }
    say() {
        console.log("你是一只", this.name)
    }
}
let dog = new Dog()
dog.say()
上一篇下一篇

猜你喜欢

热点阅读