es6中的面向对象:class 的简单用法

类似 $.ajax({xxx}) new Vue({xxx}) 。。。 有了es6的简单语法,自己也可以封装一个

class Person{
    //构造函数
    constructor(country){
        this.country=country
    }
}

class Chinese extends Person{ //继承自Person类
    constructor(name){
        super("中国") //调用父类构造函数
        this.name=name
    }
    sayHellow(){ //多了一个sayHellow方法
        console.log(`我叫${this.name},来自${this.country}`);
    }
}
class American extends Person{ //继承自Person类
    constructor(obj){ //传进来一个对象
        super("America")
        this.name=obj.name
    }
    sayHellow(){
        console.log(`My name is ${this.name} from ${this.country}`);
    }
}
const chinese = new Chinese("胡歌")//实例化Chinese对象
chinese .sayHellow() //调用实例的sayHellow方法
const american =new American({
    name:"Jack"
})
american.sayHellow()
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。