javascript--Object-Oriented programming (4)

Source: Internet
Author: User

1<script type= "Text/javascript" >2         //1. Inheritance3         //2. Prototype Chain4         //3. Borrowing the constructor function5         //4. Combination Inheritance6         //5. Prototype-Type Inheritance7         //6. Parasitic inheritance8 9         //1. Inheritance is the most fascinating concept in oo. Many OO languages support two ways of inheriting: interface inheritance and implementation inheritance. Ten         //interface inheritance supports only method signatures, while implementing inheritance inherits the actual methods.  One         //because the function is not signed, the interface inheritance cannot be implemented in ECMAScript, only the implementation of inheritance is supported, and its implementation is mainly implemented by the prototype chain.  A  -         //2, prototype chain.  -         //briefly review the relationship of constructors, prototypes, and instances: each constructor has a prototype object, and the prototype object contains a pointer to the constructor the         //The instance contains an internal pointer to the prototype object.  -         //and what happens if we let the prototype object point to an instance of another type? Obviously, the prototype object at this point will contain a pointer to another prototype. -         //correspondingly, another prototype contains a pointer to another constructor. If another prototype is another type of instance, then the relationship is still true, -         //it forms the chain of examples and prototypes. This is the basic concept of the so-called prototype chain. Eg: +  -         //Animal Class +         functionAnimal () { A              This. Shout = "not known"; at         } -Animal.prototype.getAnimalValue =function () { -             return  This. Shout; -         } -  -         //Dog Class in         functionDog () { -              This. dogshout = "Wang Woo ~"; to         } +  -         ///Dog class inherit animal class theDog.prototype =NewAnimal (); *  $Dog.prototype.getDogValue =function () {Panax Notoginseng             return  This. dogshout; -         } the  +         varInstance =NewDog (); A         //How dogs call animal classes the alert (Instance.getanimalvalue ()) +         //Instance.getanimalvalue () will go through 3 steps, 1, search instance 2, search Dog.prototype 3, search Animal.prototype -         //A, don't forget the default prototype. Remember that the default prototype for all functions is an instance of object, so the default prototype will have an internal pointer, $         //point to Object.prototype. This is also the root cause of the methods that the custom class inherits from ToString () and valueof () $  -  -         //B. Determine the relationship between prototypes and instances theAlert (instanceinstanceofObject);//true -Alert (instanceinstanceofAnimal);//trueWuyiAlert (instanceinstanceofDOG);//true the  -  WuAlert (Object.prototype.isPrototypeOf (instance));//true -Alert (Animal.prototype.isPrototypeOf (instance));//true AboutAlert (Dog.prototype.isPrototypeOf (instance));//true $  -         //C, the problem of the prototype chain -         //as you can remember, prototypes that were previously described with reference type values are shared by all instances . -         //This is why you want to define the property in the constructor, not in the prototype object.  A         //Therefore, few people in practice use the prototype chain alone.  +  the  -         //3. Borrowing constructors (also called: Forged objects or classic inheritance) $         //The way to inherit this is simply to call the superclass constructor inside the subclass constructor using the Invoke or apply the         functionAnimal () { the              This. colors = [' Red ', ' blue ', ' green ']; the         } the  -         functionDog () { in             //inherits the Animal class theAnimal.call ( This); the         } About  the         varDog =NewDog (); theDog.colors.push ("Black"); thealert (dog.colors);//' red ', ' blue ', ' green ', ' black ' +  -         varDOG2 =NewDog (); thealert (dog2.colors);//' red ', ' blue ', ' green 'Bayi  the         //compared to the prototype chain, the borrowing of constructors has a great advantage, that is, you can pass parameters to the superclass in the subclass constructor the         functionAnimal2 (name) { -              This. Name =name; -         } the  the         functionDog2 () { the             //inherits the Animal class theAnimal2.call ( This, "rhubarb"); -              This. Age = 2; the         } the  the         varDog =NewDog2 ();94Alert ("Age:" + Dog.age + ", Name:" +dog.name); the  the         //because the methods are defined in the constructor, there is no way to reuse the function. And the methods defined in the superclass prototype are not visible to subtypes. the         //The result is that all types can only use the constructor pattern. 98         //With these problems in mind, the technique of borrowing constructors is seldom used alone.  About  -         //4. Combination Inheritance101         functionAnimal3 (name) {102              This. Name =name;103              This. colors = [' Red ', ' blue ', ' green '];104         } the 106Animal3.prototype.sayName =function () {107Alert This. Name)108         }109  the         functionDog3 (name, age) {111             //Inheritance Properties theAnimal3.call ( This, name);113              This. Age =Age ; the         } the         //Inheritance Method theDog3.prototype =NewAnimal3 ();117Dog3.prototype.constructor =Dog3;118Dog3.prototype.sayAge =function () {119Alert This. Age) -         }121 122         varD3 =NewDog3 ("Xiao Huang", 1);123D3.colors.push ("Black");124alert (d3.colors);//Red,blue,green,black theD3.sayage ();//1126D3.sayname ();//Xiao Huang127  -         //combined inheritance avoids the defects of the prototype chain and borrowing constructors, blends their advantages and becomes the most commonly used inheritance method in JavaScript .129         //and instanceof and isprototypeof can also be used to identify objects that are created based on composite inheritance.  the</script>

javascript--Object-Oriented programming (4)

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.