Home > Web Front-end > JS Tutorial > Examples of JavaScript dynamic generation methods_javascript skills

Examples of JavaScript dynamic generation methods_javascript skills

WBOY
Release: 2016-05-16 18:49:35
Original
935 people have browsed it
Copy code The code is as follows:

function User(properties){
for(var i in properties ){ //Traverse all properties of the object and ensure that they work correctly
(function(which){
var p=i;
which["get" p]=function(){ / /Dynamicly generated method
return properties[p]; //Return the property value of the object
};
which["set" p]=function(val){ //Dynamicly generated method
properties[p]=val;
};
})(this); //Self-executing function, this here represents the user object instance
}
}

var user=new User({
name:"Bob",
age:44
});

alert(user.name==null); //Note: the name attribute is not Does not exist because it is a private variable of the attribute object

user.setname("Supersha"); //Call the dynamically generated object and modify the value of the attribute object
alert(user.getname()) ; //Call the dynamically generated object to get the value of the attribute object
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template