JavaScript Object-oriented programming (OOP) (c)--aggregation

Source: Internet
Author: User

Before I wrote the class and the prototype, here again the aggregation, before writing about the aggregation, and inheritance I summed up again. There are three ways to inherit in JavaScript, two are written before, but there is no explanation, here are some additional explanations.

1. Class inheritance : By invoking the constructor of the parent class within the function object, you get the properties and methods of the parent class. Use CALLH and apply callbacks primarily

1 varperson =function(){2          This. Age = 16;3          This. Sayname =function(){4Alert This. name+ This. age);5         }          6 }7 varStudent =function(){8          This. Name = ' Bote ';9Person.call ( This);Ten } One varBote =Newstudent (); ABote.sayname ();

2. Prototype inheritance : By prototype attribute inheritance, this is described in detail in the previous article, which is not described here

3. Aggregation : Sometimes, we do not need strict inheritance, what is really needed is a class (or a few classes) in some of the function methods, we need to use the aggregation.

Before you introduce aggregations, you need to say something about the Mixin Classes, which is a technique that allows you to reuse code without the need for strict inheritance. This class is used as a template for extended classes.

 //prepare the function that will be aggregated varJSON = { }; Json.prototype={tojsonstring:function(){                varOutPut = [];  for(Keyinch  This) {Output.push (key+ "-+" + This[key]); }       returnOutPut; }}//make an aggregate function, that is, a meta-class, pass in two parameters, subclass and Parent classfunctionmixin (receivingclass,givingclass) {//Traverse all the prototype functions in the parent class's prototype     for(MethodNameinchGivingclass.prototype) {        //If this prototype function does not exist in the prototype of a subclass, it inherits from the parent class .        if(!Receivingclass.prototype[methodname]) {Receivingclass.prototype[methodname]=Givingclass.prototype[methodname]; }    }}//Testvarn \function(){        This. Name = "Yuan";  This. Age = 24;} O.prototype={totest:function() {alert (2); }};mixin (O,json);varA =Newo (); alert (a.tojsonstring ());//Eject Name-->yuan,age-->24,totest-->function () {alert (2);},tojsonstring-->function () {var outPut = []; For (key in this) {Output.push (key+ "-to" +this[key]);} return outPut; }

As you can see, we selectively inherit the JSON tojsonstring method through the Mixin function, and the Totest method, which is the same as the parent class method, is the prototype method itself.

This is a class that uses a function definition, and if you use a class with a direct volume definition, we only need to change the prototype in the aggregate function to __proto__, the complete aggregation method is as follows

function mixin (receivingclass,givingclass) {       for in  givingclass) {              f (!  Receivingclass.__proto__[methodname]) {              = Givingclass[methodname];   }}}

This is used for classes that are defined only by the direct volume, as follows

var o = {naem: "SDA", age:22};mixi (O,json); alert (o. tojsonstring ());

This will directly inherit the toJSONString method in the JSON prototype.

JavaScript Object-oriented programming (OOP) (c)--aggregation

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.