Ruby's Louvre

每天学习一点点算法

导航

鲜为人知的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)

我已用到我的后端框架去了!

posted on 2012-07-28 21:21  司徒正美  阅读(2274)  评论(5编辑  收藏  举报