软件开发外包介绍,seo网站关键词优化快速官网,世界上有几个空间站,专业网页制作的帮手介绍
ES6 提供了更接近传统语言的写法#xff0c;引入了 Class#xff08;类#xff09;这个概念#xff0c;作为对象的模板。通过 class 关键字#xff0c;可以定义类。基本上#xff0c;ES6 的 class 可以看作只是一个语法糖#xff0c;它的绝大部分功能#xff0c;…介绍
ES6 提供了更接近传统语言的写法引入了 Class类这个概念作为对象的模板。通过 class 关键字可以定义类。基本上ES6 的 class 可以看作只是一个语法糖它的绝大部分功能ES5 都可以做到新的 class 写法只是让对象原型的写法更加清晰、更像面向对象编程的语法而已。
class shouji {constructor(brand,price) {this.brandbrand;this.priceprice}call(){console.log(我可以打电话)}
}let A new shouji(1,1999);
console.log(A)知识点
class 声明类constructor 定义构造函数初始化extends 继承父类super 调用父级构造方法static 定义静态方法和属性父类方法可以重写
静态成员
class Person{static name手机
}
let nokia new Person();
console.log(nokia.name);构造函数继承
function Phone(brand,price){this.brandbrand;this.priceprice;
}
Phone.prototype.callfunction (){console.log(我可以打电话);
}
function SmartPhone(brand,price,color,size){Phone.call(this,brand,price);this.colorcolor;this.sizesize;
}//设置子级构造函数原型
SmartPhone.prototypenew Phone;
SmartPhone.prototype.constructorSmartPhone;//声明子类方法
SmartPhone.prototype.photo function (){console.log(我可以玩游戏);
}
const chuizi new SmartPhone(锤子,2499,黑色,5.5inch)
console.log(chuizi);Class 的类继承
class Phone{constructor(brand,price) {this.brandbrand;this.priceprice;}//父类的成员属性call(){console.log(我可以打电话)}
}
class SmartPhone extends Phone{constructor(brand,price,color,size) {super(brand,price);this.colorcolor;this.sizesize;}photo(){console.log(拍照);}playGame(){console.log(打游戏);}
}
const xiaominew SmartPhone(小米,1999,黑色,4.7inch)
xiaomi.call();
xiaomi.photo();
xiaomi.playGame();子类对父类方法的重写
class Phone{constructor(brand,price) {this.brandbrand;this.priceprice;}//父类的成员属性call(){console.log(我可以打电话)}
}
class SmartPhone extends Phone{constructor(brand,price,color,size) {super(brand,price);this.colorcolor;this.sizesize;}photo(){console.log(拍照);}playGame(){console.log(打游戏);}//重写call(){console.log(我可以进行视频通话)}
}
const xiaominew SmartPhone(小米,1999,黑色,4.7inch)
xiaomi.call();
xiaomi.photo();
xiaomi.playGame();get 和 set 设置
class Phone{get price(){console.log(价格被读取了)return I LOVE YOU}set price(val){console.log(价格被修改了)return val;}
}//实例化对象
let s new Phone();
s.price12
// console.log(s.price) //其实是调用price方法