zoukankan      html  css  js  c++  java
  • several way to implement inheritance in javascript

     1 //1.prototype chain inheritance
     2     function SuperType() {
     3         this.property = true;
     4     }
     5     SuperType.prototype.getSuperValue = function () {
     6         return this.property;
     7     };
     8     function SubType() {
     9         this.subproperty = false;
    10     }
    11 
    12     //inherited from SuperType
    13     SubType.prototype = new SuperType();
    14     SubType.prototype.getSubValue = function () {
    15         return this.subproperty;
    16     };
    17 
    18     //2.constructor stealing inheritance
    19     function SuperType() {
    20         this.colors = ["red","blue","green"];
    21     }
    22     function SubType() {
    23         //inherited from SuperType
    24         SuperType.call(this);
    25     }
    26 
    27     //3.combination inheritance
    28     function SuperType(name) {
    29         this.name = name;
    30         this.colors = ["red", "blue", "green"];
    31     }
    32     SuperType.prototype.sayName = function () { alert(this.name); };
    33     function SubType(name,age) {
    34         //inherit properties
    35         SuperType.call(this, name); //second call superType()
    36         this.age = age;
    37     }
    38     //inherit method
    39     SubType.prototype = new SuperType(); //first call superType()
    40     SubType.prototype.sayAge = function () { alert(this.age); };
    41 
    42     //4.prototypal inheritance in javascript
    43     function object(o) {
    44         function F() { }
    45         F.prototype = o;
    46         return new F();
    47     }
    48 
    49     //5.parasitic inheritance
    50     function createAnother(original) {
    51         var clone = object(original);
    52         clone.sayHi = function () { alert("hi"); };
    53         return clone;
    54     }
    55 
    56     //6.parasitic combinate inheritance
    57     function inheritPrototype(subType, superType) {
    58         var prototype = object(superType.prototype);
    59         prototype.constructor = subType;
    60         subType.prototype = prototype;
    61     }
    62     function SuperType(name) {
    63         this.name = name;
    64         this.colors = ["red","blue","green"];
    65     }
    66     SuperType.prototype.sayName = function () { alert(this.name); };
    67 
    68     function SubType(name, age) {
    69         SuperType.call(this, name);
    70         this.age = age;
    71     }
    72     inheritPrototype(SubType,SuperType);
    73     SubType.prototype.sayAge = function () { alert(this.age); };
  • 相关阅读:
    linux中上传文件出现Refused to display 'http://***' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'.
    1 js中常用的操作
    1 走进并行世界
    18 java I/O 系统
    Spring项目中的数据库加密
    13/14:字符串与类型信息
    使用HttpClient访问接口(Rest接口和普通接口)
    java 队列的使用(转载)
    java锁有哪些类(转)
    J2EE,J2SE,J2ME,JDK,SDK,JRE,JVM区别(转载)
  • 原文地址:https://www.cnblogs.com/ongoing/p/3078987.html
Copyright © 2011-2022 走看看