简单理解js es6中class只是语法糖
class只是语法糖,但本质上依旧是基于原型的继承
function Phone(brand, price){ this.brand = brand; this.price = price; } Phone.prototype.call = function(){ console.log("打电话"); } let Huawei = new Phone("华为",5999); Huawei.call(); console.log(Huawei);
class写法
//class class Phone{ //构造方法 只能叫constructor constructor(brand, price){ this.brand = brand; this.price = price; } // 方法只能用该语法 方法名+()+{}的形式 call(){ console.log("打电话"); } } let Huawei = new Phone("华为",5999); console.log(Huawei);