ES6 的class类 笔记

class Person{  
    // 构造  
    constructor(x,y){  
        this.x = x;  
        this.y = y;  
    }  
    toString(){  
        return (this.x + "的年龄是" +this.y+"");  
    }  
}  
export {Person};  
//index.js  
import {Person} from './Person';  
let person = new Person('张三',12);  
console.log(person.toString());</span>  

Person 是一个类 ,定义了属性(x,y)与方法 toString()

console.log(typeof Person);//function  
console.log(Person === Person.prototype.constructor);//true

so: 类的数据类型就是函数,类本身就指向构造函数, 可以理解为:类就是构造函数 , 因为es5的中 【构造函数 === 构造函数.prototype.constructor】

构造函数的prototype属性,在ES6的“类”上面继续存在。事实上,类的所有方法都定义在类的prototype属性上面,通过以下方式可是覆盖类中的方法,当然定义类的时候也可以通过这个方式添加方法。

class类已经定义不可修改,不可修改Person.prototype,拓展class应该再新建的class上拓展,新class继承老的class

猜你喜欢

转载自www.cnblogs.com/liujinyu/p/9075949.html