Es6中的Class
ES6引入了Class(类)这个概念,通过class关键字可以定义类。该关键字的出现使得其在对象写法上更加清晰,更像是一种面向对象的语言。
私有属性和方法和公有属性声明方法
class Fn{
constructor(x) {// 1.私有属性和方法
this.x = x
this.Hello = function (){
}
}
// 2.私有的属性
aa = 123;
//公有方法
getX() {
console.log(this.x);
}
}
//公有属性
A.prototype.aa = 456;
ES6中的继承(和寄生组合继承一样都是最完美的继承
通过extends来实现的继承
ES6继承 ==>class 子类构造函数 extends 父类构造函数{}
class Parent {
constructor(x) {
this.x = x;
this.Hello = function () {
console.log("Hello")
}
}
getX() {
console.log("getX==>", this.x)
}
}
class Child extends Parent {
//如果不写constructor,不会报错,继承会正常继承
//如果不写constructor浏览器会自动的帮我们去创建以下代码
// constructor(...args){
// super(...args)
// }
constructor(y) {
super(100);// Parent.call(this,100)
//如果是通过继承的形式写了constructor,那么就必须使用super来继承父类的私有属性和方法,
//不然就会报错
this.y = y;
}
getY(){
console.log("getY==>", this.y)
}
}
let child = new Child(200)