JavaScript动态生成访问方法
function Person(properties) { for (var item in properties) { //必须放在匿名函数内分离作用域,不然每个p会是最后一项item的值 (function (which) { //记住这个作用域内的值 var p = item; which["Get" + p] = function () { return properties[p]; }; which["Set" + p] = function (val) { properties[p] = val; }; })(this); } } var user = new Person({ Name: "Bob", Age: 44}); user.SetName("jack"); user.SetAge(20); alert(user.GetName() + " " + user.GetAge());