Copy codeThe Code is as follows:
Function User (properties ){
For (var I in properties) {// traverses all attributes of the object and ensures that it acts correctly
(Function (which ){
Var p = I;
Which ["get" + p] = function () {// dynamically generated Method
Return properties [p]; // return the object property value
};
Which ["set" + p] = function (val) {// dynamically generated Method
Properties [p] = val;
};
}) (This); // self-executed function. this indicates the user object instance.
}
}
Var user = new User ({
Name: "Bob ",
Age: 44
});
Alert (user. name = null); // Note: The name attribute does not exist because it is a private variable of the property object.
User. setname ("Supersha"); // call the dynamically generated object to modify the attribute object Value
Alert (user. getname (); // call the dynamically generated object to obtain the attribute object Value