[JS] Inheritance Method Summary

Source: Internet
Author: User

Mode one: prototype chain inheritance (prototype Mode)
  
 
  1. function Animal(){
  2. this.species = "动物";
  3. }
  4. function Cat(name,color){
  5. this.name = name;
  6. this.color = color;
  7. }
  8. Cat.prototype = new Animal();//核心
  9. Cat.prototype.constructor = Cat;

Disadvantages:
1. The properties and methods of the reference type on the prototype are shared by all instances (personally think this should not be a disadvantage, after all, the prototype is to send this Use)
2. When you create an instance of cat, you cannot send a parameter to animal

Mode two: Classic Inheritance (constructor Binding)
  
 
  1. function Animal(){
  2. this.species = "动物";
  3. }
  4. function Cat(name,color){
  5. Animal.call(this);//核心,或Animal.apply(this,arguments);
  6. this.name = name;
  7. this.color = color;
  8. }

Advantages:
1. Avoid the properties and methods of reference types being shared by all instances, because they are useless to prototypes.
2. When you create an instance of cat, you can pass a parameter to the animal

Disadvantages:
1. Properties and methods are defined in the constructor, and each time an instance is created, the properties and methods are created

Way Three: combination inheritance (prototype chain inheritance and classic inheritance double Swords)
  
 
  1. function Parent (name) {
  2. this.name = name;
  3. this.colors = [‘red‘, ‘blue‘, ‘green‘];
  4. }
  5. Parent.prototype.getName = function () {
  6. console.log(this.name)
  7. }
  8. function Child (name, age) {
  9. Parent.call(this, name);//核心
  10. this.age = age;
  11. }
  12. Child.prototype = new Parent();//核心
  13. var child1 = new Child(‘kevin‘, ‘18‘);
  14. child1.colors.push(‘black‘);
  15. console.log(child1.name); // kevin
  16. console.log(child1.age); // 18
  17. console.log(child1.colors); // ["red", "blue", "green", "black"]
  18. var child2 = new Child(‘daisy‘, ‘20‘);
  19. console.log(child2.name); // daisy
  20. console.log(child2.age); // 20
  21. console.log(child2.colors); // ["red", "blue", "green"]

Advantages:
1. Fusion of the advantages of prototype chain inheritance and constructors by combining the benefits of prototype chain inheritance and constructor functions

Disadvantages:
1. Two calls to the parent constructor, adding additional, unwanted properties to the child.prototype, and destroying the prototype chain

Mode Four: Parasitic combination inheritance
  
 
  1. function Parent (name) {
  2. this.name = name;
  3. this.colors = [‘red‘, ‘blue‘, ‘green‘];
  4. }
  5. Parent.prototype.getName = function () {
  6. console.log(this.name)
  7. }
  8. function Child (name, age) {
  9. Parent.call(this, name);//核心
  10. this.age = age;
  11. }
  12. Child.prototype = Object.create(Parent.prototype);//核心
  13. Child.prototype.constructor = Child;
  14. var child1 = new Child(‘kevin‘, ‘18‘);
  15. console.log(child1)

Pros: on the basis of combination inheritance, avoid creating unnecessary and unnecessary attributes on Child.prototype. At the same time, the prototype chain remains intact, so instanceof and isprototypeof can be used Normally.

Reference Links:
Https://github.com/mqyqingfeng/Blog/issues/16
http://www.ayqy.net/blog/%E9%87%8D%E6%96%B0%E7%90%86%E8%A7%A3js%E7%9A%846%E7%A7%8D%E7%BB%A7%E6%89%BF%E6%96%B9%E5%BC%8F/
Http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance.html

[JS] Inheritance Method Summary

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.