原型
原型系统
- 如果所有对象都有私有字段[[protoptype]],就是对象的原型
- 读一个属性,如果对象本身没有,则会继续访问对象的原型,直到原型为空或者找到为止。
es6
提供来了一系列内置函数,以便更为直接地访问操纵原型
- Object.create 根据指定的原型创建新对象,原型可以是null
- Object.getPrototypeOf获得一个对象的原型
- Object.setPrototypeOf设置一个新的原型
class
- 类的基本写法
//es6 类的基本写法
class Rectangle {
constructor(height, width){
this.height = height;
this.width = width;
}
}
//Getter
get area() {
return this.calcArea();
}
calcArea(){
return this.height * this.width;
}
- 类的例子
//es6类的基本例子
/*创造了Animal类,并且通过extends关键字让Dog继承了它,展示了最终调用子类的speak方法获取了父类的name*/
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(this.name + ' makes a noise');
}
}
class Dog extends Animal {
constructor(name) {
super(name);
}
speak(){
console.log(this.name + 'barks.')
}
}
let d = new Dog('Mitzie');
d.speak();