在ES6中,class (类)作为对象的模板被引入,可以通过 class 关键字定义类。
class 的本质是 function。
它可以看作一个语法糖,让对象原型的写法更加清晰、更像面向对象编程的语法。
定义个类
class Parent {
constructor(name,age){
this.name = name;
this.age =age;
}
breath(){
console.log("呼呀,吸呀!")
}
}
实例化一个类
var p = new Parent("mumu", 18)
// Parent {name: "mumu", age: 18}
p.breath();
// 呼呀,吸呀!