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); };
  • 相关阅读:
    服务器变量 $_SERVER 详解
    PHP 函数功能参考
    ecshop后台0day漏洞原理+利用方法 XSS+Getshll
    CSRF漏洞原理说明与利用方法
    Drupal 远程命令执行漏洞(CVE-2018-7600)
    SSH登陆验证绕过漏洞(cve-2018-10933)
    单元二:建立和维护数据表
    单元一:认识数据库系统
    【 模块1 认识计算机 】1.2 认识微型计算机
    【 模块1 认识计算机 】 1.1走进计算机世界
  • 原文地址:https://www.cnblogs.com/ongoing/p/3078987.html
Copyright © 2011-2022 走看看