鲜为人知的get,set操作符
IE6在大陆的肆虐,让人不敢尝试许多东西,像get,set这样的好东西就是一个例子,因为它们在IE67就是语法错误,因此ecma262v5才搞出defineProperty这样的东西。
用法很简单,让一个方法像属性那样使用!
var a = {
bb: "xxx",
get aa(){//不用写function与:
return this.bb+"!"
}
}
console.log(a.aa)
var fn = function(){
this.ee = "!!"
}
fn.prototype = {
get dd(){//不用写function与:
return this.ee+"pp"
}
}
var instance = new fn;
console.log(instance.dd)
我已用到我的后端框架去了!
机器瞎学/数据掩埋/模式混淆/人工智障/深度遗忘/神经掉线/计算机幻觉/专注单身二十五年
浙公网安备 33010602011771号