Introduction to the encapsulation of JavaScript inheritance

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.