ES6中的类和ES5中的原型的前世今生

Class是es6引入的最重要的特性之一,在没有class之前,我们只能通过原型链来模拟类

原型链的书写

var Root = function(){

};

Poot.prototype.eat = function(){

console.log("ES5 EATING");

}

Root.doing = function(){

conaole.log("ES5 DOING");

}

let a  = new Root();

a.eat(); 输出 ES5 EATING

Root.doing();  ES5 DOING

用类书写

class Roots(){

constructor(){

}

eat(){

console.log("ES6 EATING");

}

static doing(){

console.log("ES6 DOING");

}

let b = new Roots();

b.eat();

Roots.doing(); 

}

猜你喜欢

转载自blog.csdn.net/qq_38845858/article/details/81913162
今日推荐