JavaScript mode Reading Notes Chapter 1 code reuse Mode

Source: Internet
Author: User

JavaScript mode Reading Notes Chapter 1 code reuse Mode
It mainly uses code inheritance for reuse. 1. Use class inheritance. -1. class-based inheritance: inheritance is implemented based on the class method, that is, the so-called class-based inheritance.
-2. class inheritance: You can use the constructor (child) to obtain the parent attributes and create an object.

<script>//parent function Parent(name){this.name = name || 'Adam'; } Parent.prototype.say = function(){ return this.name; } //child function Child(name){  }// Implement inheritance here and implement it by yourself inherit(Child, Parent);   </script>
-3, default mode
 // Default mode function inherit(C, P){C.prototype = new P(); }  var kid = new Child();Console. log (kid. say (); // output Adam
-4. Borrow Constructor
Borrow the parent class constructor, which passes the sub-object to bind to this and forwards any parameters.
<script>// Parent Constructorfunction Article(){this.tags = ['js', 'css'];}var article = new Article(); function BlogPost(){ }BlogPost. prototype = article; // get a reference to the corresponding objectvar blog = new BlogPost(); function StaticPost(){Article. call (this); // get a copy of the corresponding object Member, equivalent to generating the same member variable in this object}var page = new StaticPost();console.log(article.hasOwnProperty('tags'));//trueconsole.log(blog.hasOwnProperty('tags'));//falseconsole.log(page.hasOwnProperty('tags'));//true   </script>
-5. Borrow and set the prototype.
<script>function Child(a, b, c, d){Parent. apply (this, arguments); // obtain the member variable of the Parent class by borrow} Child. prototype = new Parent (); // obtain the method of the Parent class through this   </script>
<script>function Parent(name){this.name = name || "Tom";}Parent.prototype.say = function(){return this.name;}function Child(name){Parent.apply(this, arguments);} Child.prototype = new Parent(); var kid = new Child("Child");console.log(kid.name);//childconsole.log(kid.say());//childdelete kid.name;console.log(kid.say());//Tom   </script>
-6. Sharing prototype
function inherit(C, P){      C.prototype = P.prototype;}
If a subclass below the inheritance chain modifies the prototype, it will affect all objects on the prototype chain. -7. Temporary Constructor
function inherit(C, P){    var F = function(){};   F.prototype = P.prototype;   C.prototype = new F();}
-8, Klass a. All have a set of conventions on how to name class methods.
B. there are classes inherited from other classes.
C. You can access the parent class or superclass In the subclass.
 <script>var klass = function(Parent, props){var Child, F, i; // 1. New ConstructorChild = function(){if(Child.uber && Child.uber.hasOwnProperty("_construct")){Child.uber._construct.apply(this, arguments);} if(Child.prototype.hasOwnProperty("_construct")){Child.prototype._construct.apply(this, arguments);}}; // 2. InheritParent = Parent || Object;F = function(){}; F.prototype = Parent.prototype;Child.prototype = new Child();Child.uber = Parent.prototype;Child.prototype.constructor = Child; // 3. Add Implementation Methodfor(i in props){if(props.hasOwnProperty(i)){Child.prototype[i]= props[i];}} return Child; };   </script>


Code reuse is the purpose, and inheritance is only one of the methods to achieve this goal.





















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.