JS object-oriented Programming: JavaScript object-oriented programming, strict process of standardized programming method, so far the best JS generation object code structure
Many JavaScript-oriented programming methods are popular on the market, many of which are problematic. Here's a summary of the most correct JavaScript object-oriented programming patterns.
For class Special inheriting from class Common inheriting from class Super, the creation of a Special object, in detail, should go through the following steps.
1 Determining the inheritance Relationship
1.1 Read Special's parent class, found to be Common
1.2 Read Common's parent class, found to be Super
1.3 Read Super's parent class, found no (invisible parent Object)
2 Load class structure (if not loaded). Loading is not required to load repeatedly)
2.1 Load Super, Common, Special class properties and Class methods
2.2 What attributes are loaded on the Super class object and which methods
2.3 What attributes are loaded on the Common class object, and which methods
2.4 What attributes are loaded on the Special class object, and which methods
2.5 to Super, Common, Special class Property allocation space
2.6 To Super, Common, Special class Property assignment
3 Allocating heap Space
3.1 Space to allocate super, including space for all super members
3.2 Allocate Common space, including Common of all members
3.3 Allocate Special space, including Special of all members
4 Initializing objects
4.1 Constructing method of calling Super
4.2 Constructing method of calling Common
4.3 Constructing method of calling Special
So, how do you use JavaScript to create objects reasonably and strictly according to the standard object build order?
1 Determine the inheritance relationship. Because JavaScript has no variable type bindings, it should be possible to allow multiple inheritance. The first parent class is the primary parent.
Super._super = [];
Common._super = [Super];
Special._super = [Common];
2 Load class structure
This involves a technique that people familiar with JavaScript object-oriented programming will know well, called prototype chains (prototype chaining). The JavaScript object-oriented class structure is actually saved by the prototype object of each class. Each generated object is based on a prototype object, making a new object with the same members and member values. If you directly make a prototype object of a class equal to an object of its parent class, it is equivalent to copying all the members of the parent class prototype into the current class prototype. These members, in addition to displaying visible surprises, include some system member variables, such as class name attributes for instanceof, which enable the subclass member to be implemented instanceof
The parent class name, which results in true--this attribute can be passed to the next level of subclasses.
In addition to the master inheritance formed by the prototype chain, there is no concept of an interface because JavaScript does not have the concept of a reference type. Therefore, you need to manually load the remaining parent class members except the main parent class. By means of for (var xxx in XXX), you can remove members from the remaining parent prototypes and use them to establish the class structure of the current class. It should be noted here that if the various parent classes have the exact same name, then the method that is the subclass of the last loaded in. This has to be said to be a pity for JavaScript object-oriented multiple inheritance. This article links http://www.cxybl.com/html/wyzz/JavaScript_Ajax/20120608/28900.html
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.