/**
* When this function is called, only the first parameter is passed in. If the second one does not exist, the class is created
* When this function is called, two parameters are passed in, the first one The parameter is the base class, and the second parameter adds content based on the base class
*/
function extend(obj ,prop){
function F(){
}
//If the first parameter is of object type (i.e. json object), assign the key value of json to the F function Prototype F.prototype.key = value
if (typeof(obj) == "object") {
for(var name in obj){
F.prototype[name] = obj[name];
}
} else {//If the first parameter is of function type, assign the prototype of the secondary function to the F function. Prop must be passed by value (json object), so assign prop to F Prototype of function
F.prototype = obj.prototype;
for(var name in prop){
F.prototype[name] = prop[name];
}
}
return F;
}
//Because there is only one parameter here, the key and value of the json object will be assigned to the prototype of the F function in the extend function, and then the person variable will be used to receive the F function. This When person also becomes a function, this function also has the prototype name and sex of the F function
var person = extend({
name: "xxc",
sex: "man"
} );
//Pass the person function in. After entering the extend function, first assign the person prototype to the F function, and then assign the second parameter hope: "more money" to the F function. At this time, the F function The prototype has three things name, sex, hope
//Finally, the F function is returned, causing the prototype of person to be the same as the F function, with name, sex, hope
var person = extend(person,{
hope:"more money"
});
alert(person.prototype.name);//xxc
alert(person.prototype.sex);//man
alert(person.prototype .hope);//more money
function.html< /title>
Statement: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