JavaScript programming for OOP

Source: Internet
Author: User

1. First the single inheritance of JS is realized by a function. To implement inheritance using a prototype approach

1(function () {2 3 $.extend ({4 OOP: {5Extendfunction(Child, father) {6                 if(father = =NULL) {7                     varFather =function () { };8Console.log ( This);9Father.prototype = ThisTen                 } OneChild.prototype =Newfather (); A  -Child.base =father; -Child.prototype.base =Child.prototype; the             } -         } -  -     }); +Window.oop =$.opp; -})();
View Code

This method appends the base instance property and the static instance property to the subclass to find the parent class.

2. Create a base class.

1(function () {2Oop.ui =function () {3         /**4 * Before rendering5 * The base class does not turn on rendering. 6         */7          This. renderbefor =function(self) {8             return false;9         }Ten         /* One * Render function A         */ -          This. Render =function () { -             if( This. renderbefor () = =false)return; the             if( This. Element) { -                  This. Element.wrap ("<span class= ' oop_ui ' ></span>"); -             } -              This. redered (); +         } -          This. redered =function(self) { +  A         } at     } - oop.extend (oop.ui); -})();
View Code

3. Create a subclass.

1(function () {2Oop.ui.select =function($input) {3 4          This. Element =$input5 6          This. renderbefor =function () {7             return true;8         }9          This. Render =function () {Ten             /** One * Use the Apply method to invoke the base class's Render method.  A             */ -              This. base.render.apply ( This, arguments); -              This. Element.parent (). addclass ("Oop-ui-select"); the         } -     }; - oop.extend (Oop.ui.select, oop.ui); -  +})();
View Code

Subclass Oop.ui.select in the Render method.

The method of calling the base class is this.base.render.apply (This,arguments);

Apply is to overwrite this in the Base.render () method as this of the current subclass (that is, point to oop.ui.select).

JavaScript programming for OOP

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.