JavaScript's "prototype Nectar"

Source: Internet
Author: User

Today to discuss with friends JS object-oriented programming problem, think of the original once read an article, but looked at a long time to think not up, with a lot of keywords, and finally with the "understanding of JavaScript Object-oriented" The two keywords found the original text address:/HTTP Www.cnblogs.com/zhangshiwen/p/3627085.html

In order to prevent the future difficult to see such a good article, the original text of the most valuable "prototype dew" a piece of content posted.

//syntax Nectar:    varObject =//defines a lowercase object base class for implementing the most basic methods, etc.{isA:function(Atype)//a basic method for judging the relationship between a class and a class and between an object and a class        {            varSelf = This;  while(self) {if(Self = =atype)return true; Self=Self .            Type;            }; return false;        }    }; functionClass (Abaseclass, Aclassdefine)//Create a function of a class to declare classes and inheritance relationships    {        functionClass_ ()//Create a temporary function shell for a class        {             This. Type = Abaseclass;//We contract a type property to each class, referencing its inherited class             for(varMemberinchaclassdefine) This[member] = Aclassdefine[member];//copy all definitions of a class to the currently created class        }; Class_.prototype=Abaseclass; return NewClass_ ();        }; functionNew (AClass, Aparams)//function for creating objects, for object creation of any class    {        functionNew_ ()//Create a temporary function shell for an object        {             This. Type = AClass;//We also contract a Type property for each object, which allows access to the class to which the object belongs            if(aclass.create) aClass.Create.apply ( This, aparams);//we agreed that all class constructors are called Create, which is similar to Delphi .        }; New_.prototype=AClass; return NewNew_ ();    }; //application effect of the syntax nectar:    varPerson = Class (object,//derive to object base class{Create:function(name, age) { This. Name =name;  This. Age =Age ; }, SayHello:function() {alert ("Hello, I ' m" + This. Name + "," + This. Age + "years-old.");        }    }); varEmployee = Class (person,//derives from the person class, is it similar to the General object language? {Create:function(name, age, salary) {Person.Create.call ( This, name, age);//call the constructor of the base class             This. Salary =salary; }, Showmethemoney:function() {alert ( This. Name + "$" + This. Salary);    }    }); varBillGates = New (person, ["Bill Gates", 53]); varStevejobs = New (Employee, ["Steve Jobs", 53, 1234]);    Billgates.sayhello ();    Stevejobs.sayhello ();        Stevejobs.showmethemoney (); varLittlebill = New (Billgates.type, ["Little Bill", 6]);//Create Littlebill based on the type of billgateLittlebill.sayhello ();       Alert (Billgates.isa (person)); //trueAlert (Billgates.isa (Employee));//falseAlert (Stevejobs.isa (person));//trueAlert (Person.isa (Employee));//falseAlert (Employee.isa (person));//true

"Syntax nectar" not too much, as long as a little bit, you can change the entire code legibility and fluency, so that the code appears more elegant. With these syntax nectar, JavaScript is very much like the general object language, writing code and feeling more cool!

Happily, JavaScript programs that are nourished by these nectar are more efficient. Because there are no useless object-level members in the prototype object, and there is no constructor attribute body, there is less connection with the constructor function, but still maintain the sharing of the method. This makes JavaScript much less time-consuming to trace back to the prototype chain and search for properties and methods.

Let's call this form the "Nectar model"! In fact, this "dew Model" prototype usage is in line with the prototype concept of the original meaning of the JavaScript prototype is true!

Presumably Microsoft design AJAX architecture Engineers see this dew model, certainly regret not to have the Ajax department moved from the United States to our China's Guanyin temple, miss the goddess of mercy. Of course, we can only be in the code example, Bill Gates as an object to play, really let him give up God turned to my Buddha is certainly not easy, the opportunity has not arrived Ah! If one day you see this kind of dew model in Microsoft's new Ajax class library, that is the true fate!

JavaScript's "prototype Nectar"

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.