Copy codeThe Code is as follows:
/**
* When this function is called, the class is created only when the first parameter is passed in and the second parameter does not exist.
* When this function is called, two parameters are passed in. The first parameter is the base class, and the second parameter is added based on the base class.
*/
Function extend (obj, prop ){
Function F (){
}
// If the first parameter is of the object type (json object), The json key value is assigned 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 the function type, the prototype of the next function is assigned to the F function. prop must be a value (json object ), so assign the prop value to the prototype of the F 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 F function prototype in the extend function, and then the F function will be received using the person variable, at this time, person also becomes a function, which also has the prototype name and sex of the F function.
Var person = extend ({
Name: "xxc ",
Sex: "man"
});
// Pass in the person function. 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 prototype of the F function has three things: name, sex, hope
// Finally, the F function is returned. As a result, the prototype of the person is the same as that of the F function, which includes name, sex, and hope.
Var person = extend (person ,{
Hope: "more money"
});
Alert (person. prototype. name); // xxc
Alert (person. prototype. sex); // man
Alert (person. prototype. hope); // more money
Copy codeThe Code is as follows:
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> function.html </title>
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "description" content = "this is my page">
<Meta http-equiv = "content-type" content = "text/html; charset = UTF-8">
<Script src = "../jquery/jquery-1.10.2.min.js"> </script>
<Script src = "extends2.js"> </script>
<! -- <Link rel = "stylesheet" type = "text/css" href = "./styles.css"> -->
</Head>
<Body>
</Body>
</Html>