cocos2d JS,JavaScript 动态生成方法的例子

 1 function User(properties) {
 2             for (var i in properties) {
 3                 (function (which) {
 4                     var p = i;
 5                     which["get" + p] = function () {
 6                         return properties[p];
 7                     }
 8                     which["set" + p] = function (val) {
 9                         properties[p] = val;
10                     }
11                 })(this);
12             }
13         }
14         var user = new User({
15             name: "angela",
16             age: 26
17         });
18         alert(user.name == null);//true   //name属性并不存在  因为它是属性对象的私有变量
19         alert(user.getname() == "angela");//true  //使用新的getname方法来获得这个值  这个函数是动态生成的
20         user.setage(22);
21         alert(user.getage() == 22);//true