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我会九阳神功
网友评论