JavaScript Simple inheritance mechanism implementation (not using prototype and new)

Source: Internet
Author: User

This method is not the author's original, the author is only on the basis of predecessors, to summarize, to obtain a concise and practical JavaScript inheritance method.

Traditional JavaScript inheritance is based on the prototype prototype chain, and requires a lot of new operations, the code is not concise, the readability is not very strong, it seems to be vulnerable to the prototype chain pollution.

The author summarizes the way of inheritance, concise and clear, although not the best way, but hope to bring inspiration to the reader.

All right, no more nonsense, just look at the code, the comments are detailed, you can understand ~ ~ ~

1 /**2 * Created by Yang Yuan on 14-11-11.3 * Do not use prototype to implement inheritance4  *5  */6 7 /**8 * JavaScript Object Copy, copy only one layer, and only copy function properties, not universal! 9 * @param obj objects to be copiedTen * @returns Object One  */ AObject.prototype.clone =function(){ -     var_s = This, -NEWOBJ = {}; the_s.each (function(key, value) { -         if(Object.prototype.toString.call (value) = = = "[Object Function]"){ -Newobj[key] =value; -         } +     }); -     returnNEWOBJ; + }; A  at /** - * Traverse obj all its own properties -  * - * @param callback callback function. Callback will contain two parameters: Key property name, Value property -  */ -Object.prototype.each =function(callback) { in     varKey = "", -_this = This; to      for(Keyinch_this) { +         if(Object.prototype.hasOwnProperty.call (_this, key)) { - callback (key, _this[key]); the         } *     } $ };Panax Notoginseng  - /** the * Create sub-class + * @param ext obj, which contains methods that need to be rewritten or extended.  A * @returns Object the  */ +Object.prototype.extend =function(EXT) { -     varChild = This. Clone (); $Ext.each (function(key, value) { $Child[key] =value; -     }); -     returnChild ; the }; - Wuyi /** the * Create object (instance) - * @param arguments can accept any number of parameters as a constructor parameter list Wu * @returns Object -  */ AboutObject.prototype.create =function(){ $     varobj = This. Clone (); -     if(obj.construct) { - obj.construct.apply (obj, arguments); -     } A     returnobj; + }; the  -  $ /** the * useage Example the * The use of this method of inheritance avoids cumbersome prototype and new.  the * However, this example, which I am writing now, can only inherit the function of the parent class (which can be understood as a member method).  the * If you want to inherit richer content, refine your clone method.  -  * in  * the  */ the  About /** the * Animal (parent Class) the * @type {{construct:construct, eat:eat}} the  */ + varAnimal = { -Constructfunction(name) { the          This. Name =name;Bayi     }, theEatfunction(){ theConsole.log ("My name is" + This. name+ ". I can eat! "); -     } - }; the  the /** the * Birds (sub-category) the * Birds rewrite the parent class eat method and extend the Fly method - * @type {subclass |void} the  */ the varBird =Animal.extend ({ theEatfunction(food) {94Console.log ("My name is" + This. name+ ". I can eat "+food+"! "); the     }, theFlyfunction(){ theConsole.log ("I can fly!");98     } About }); - 101 /**102 * Create Bird instances103 * @type {Jim}}104  */ the varBirdjim = Bird.create ("Jim"),106Birdtom = Bird.create ("Tom");107 108Birdjim.eat ("worm");//My name is Jim. I can eat worm!109Birdjim.fly ();//I can fly! the 111Birdtom.eat ("Rice");//My name is Tom. I can eat rice! theBirdtom.fly ();//I can fly!

JavaScript Simple inheritance mechanism implementation (not using prototype and new)

Related Article

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.