es5和es6创建类的方法

2019-11-11  本文已影响0人  随风飞2019

es5
组合使用构造函数+原型模式
function Person(name,age){
this.name=name;
this.age=age;
}

Person.prototype.say=function(val){
console.log(this.name+val);
}

var p=new Person('张三丰',25);
p.say("我会太极八卦。")
//张三丰我会太极八卦。

es6使用Class类实现

class Person{
constructor(name,age){
this.name=name;
this.age=age;
}
say(val){
console.log(this.name+val)
}
}

let wuji = new Person("zhangwuji",32);
wuji.say("我会九阳神功")
//zhangwuji我会九阳神功

上一篇 下一篇

猜你喜欢

热点阅读