Test1 是在 原型链 上定义的;
Test2 是用 ES6 中定义的;
// Test1function Test1(val) {
alert("1"); this.value = val;
}
Test1.prototype = {
get value() {
alert("2"); return this._value;
},
set value(val) {
alert("3"); this._value = val;
}
};var f1=new Test1("zj");
f1.value="sdf";console.log(f1);运行结果是:
弹出: 1 3 3 2;
输出:
[object Object] { _value: "sdf", value: "sdf"}// Test2class Test2{ constructor(val){
alert("1"); this.value = val;
}
get value(){
alert("2"); return this._value;
}
set value(v){
alert("3"); this._value=v;
}
}var f2=new Test2("zj");
f2.value="sdf";console.log(f2);运行结果是:
弹出: 1 3 3;
输出:
[object Object] { _value: "sdf"}
呼啦一阵风
随时随地看视频慕课网APP
相关分类