es6的面向对象

es6的classes

组织要领:constructor。new的时刻挪用
class Student{
    constructor (name,age){
        this.name=name;
        this.age=age;
    }
    run(){
        return this.name;
    }
}
let xs = new Student("姜姜",23);
console.log(xs.name);
console.log(xs.age);

function Vehicle (name,age){
    this.name = name;
    this.age = age;
}
Vehicle.prototype.name = function name(){
    return this.name;
};
var jj = new Vehicle ("吴建",24);
console.log(jj.name);
get set
class Student{
    constructor (name,age){
        this.name =name;
        this.age=age;
    }
    run(){
        console.log("我会飞");
    }
    get xm(){
        return this.name +"123";
    }
    set xm(value){
        this.name =value;
    }
    static shangxue (){
        console.log("去上学");
    }
}
let xs = new Student("姜姜",25);
console.log(xs.xm);
xs.xm="姜姜";
console.log(xs.xm);
Student.shangxue();
//get:猎取加赋值。
//set:设置。
//static:静态要领|类要领。
//set和get的要领名雷同,而且能够同名
要领重载|要领掩盖:
class Student{
    constructor (name,age){
        this.name =name;
        this.age=age;
    }
    run(){
        console.log("我会飞");
    }
}
let xs = new Student("姜姜",25);

class Teacher extends Student{
    constructor (name,age,sex){
        super(name,age);
        this.sex=sex;
    }
    eat(){
        console.log(this.name +"is eating")
    }
    run(){
        super.run();
        console.log("我想高飞");
    }
}
var ls = new Teacher("吴建","30","男");
ls.run();//我会飞 我想高飞;
解释:虽然子类继续了父类的run要领,然则子类会把父类的要领给掩盖掉,这个就是要领掩盖
继续export
定名范例 —name 私有属性
static静态要领 要领掩盖
ES6 中有 class 语法。值得注重是,这里的 class 不是新的对象继续模子,它只是原型链的语法糖表现形式。
extends 许可一个子类继续父类,须要注重的是,子类的 constructor 函数中须要实行 super() 函数。
关键字 class, extends, super
特性
  1. 非声明提拔(hoisted) 和let一样

  2. 自动处于严厉形式

  3. 须要new, 否则会抛错

  4. 重写类名和要领会抛错

  5. 有get set 要领

  6. 能够指定要领为static 。只能在class内部运用。

    原文作者:姜筱妍
    原文地址: https://segmentfault.com/a/1190000010225760
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞