zoukankan      html  css  js  c++  java
  • javascript 继承实现方法

    1. [代码][JavaScript]代码     
    //1、对象冒充
    //说明:构造函数使用this关键字给所有属性和方法赋值(即采用类声明的构造函数方式)。因为构造函数只是一个函数,所以可使ClassA的构造函数成为ClassB的方法,然后调用它。ClassB就会收到ClassA的构造函数中定义的属性和方法。
    function ClassA(sColor) {
    this.color = sColor;
    this.showColor = function() {
    alert(this.color);
    };
    }


    function ClassB(sColor, sName) {
    this.newMethod = ClassA;
    this.newMethod(sColor);
    delete this.newMethod;


    this.name = sName;
    this.showName = function() {
    alert(this.name);
    };
    }


    var oa = new ClassA("RED");
    var ob = new ClassB("blue", "red");
    oa.showColor();
    // RED
    ob.showColor();
    // blue
    ob.showName();
    // red
    2. [代码][JavaScript]代码     
    //2、call()方法
    //说明:这是与经典的对象冒充方法最相似的方法。它的第一个参数用作this的对象。其他参数都都直接传递给函数自身。其实就相当于前一个是对象,后一个是普通的参数一样。
    function showColor(sPrefix, sSuffix) {
    alert(sPrefix + this.color + sSuffix);
    };
    var obj = new Object();
    obj.color = "red";
    // the color is red ,a very nice color indeed
    showColor.call(obj, "the color is ", " ,a very nice color indeed");
    function ClassA(sColor) {
    this.color = sColor;
    this.showColor = function() {
    alert(this.color);
    };
    }


    function ClassB(sColor, sName) {
    ClassA.call(this, sColor);
    this.name = sName;
    this.showName = function() {
    alert(this.name);
    };
    }


    var ob = new ClassB("red", "ooo");
    ob.showColor();
    // red
    ob.showName();
    // ooo
    3. [代码][JavaScript]代码     
    //3、apply()方法
    //说明:有两个参数,用作this的对象和要传递给函数的参数的数组。
    function showColor(sPrefix, sSuffix) {
    alert(sPrefix + this.color + sSuffix);
    };http://www.huiyi8.com/jiaoben/
    var obj = new Object();
    obj.color = "green";
    // the color is green ,a very nice color indeed
    showColor.apply(obj, new Array("the color is ", " ,a very nice color indeed"));
    function ClassA(sColor) {
    this.color = sColor;
    this.showColor = function() {
    alert(this.color);
    };
    }


    function ClassB(sColor, sName) {
    ClassA.apply(this, arguments);
    this.name = sName;
    this.showName = function() {
    alert(this.name);
    };
    }


    var ob = new ClassB("red", "ooo");
    ob.showColor();
    // red
    ob.showName();
    // ooo
    4. [代码][JavaScript]代码     
    //4、原型链
    // 说明:原型链扩展了类的原型定义方式。prototype对象是个模板,要实例化的对象都以这个模板为基础。prototype对象的任何属性和方法都被传递给那个类的所有实例。原型链利用这种功能来实现继承机制。原型链的弊端是不支持多重继承。记住,原型链会用另一种类型的对象重写类的prototype属性。因此子类的所有属性和方法必须出现在prototype属性被赋值以后。
    // 原型链方式对应原型方式
    function ClassA() {
    }


    ClassA.prototype.color = "red";
    ClassA.prototype.showColor = function() {
    alert(this.color);
    };
    function ClassB() {
    }


    ClassB.prototype = new ClassA();
    ClassB.prototype.name = "redred";
    ClassB.prototype.showName = function() {
    alert(this.name);
    };
    var objA = new ClassA();
    var objB = new ClassB();
    objA.color = "blue";
    objA.showColor();
    // blue
    objB.color = "green";
    objB.name = "dodo";
    objB.showColor();
    // green
    objB.showName();
    // dodo
    alert( objB instanceof ClassA);
    // true
    alert( objB instanceof ClassB);
    // true
    5. [代码][JavaScript]代码     
    //5、混合方式
    //说明:与定义类时同样的方式,即利用构造函数方式定义属性,用原型方式定义方法。而在继承机制中,可以用对象冒充继承构造函数的属性,用原型链继承prototype对象的方法。
    function ClassA(sColor) {
    this.color = sColor;
    }


    ClassA.prototype.showColor = function() {
    alert(this.color);
    };
    function ClassB(sColor, sName) {
    ClassA.call(this, sColor);
    this.name = sName;
    }


    ClassB.prototype = new ClassA();
    ClassB.prototype.showName = function() {
    alert(this.name);
    };广告代码
    var objA = new ClassA("BLUE");
    objA.showColor();
    // BLUE
    var objB = new ClassB("red", "baba");
    objB.showColor();
    // red
    objB.showName();
    // baba

  • 相关阅读:
    从零开始搭建Wpf基础9-使用MaterialDesignToolkit对界面进行美化下
    从零开始搭建Wpf基础8-登录界面成功后显示主窗体
    从零开始搭建Wpf基础7-Api数据接入
    从零开始搭建Wpf基础6-Tab选项卡MVVM实现
    Wpf下dragablz使用Prism8进行导航-3
    从零开始搭建Wpf基础5-无限级菜单MVVM实现
    从零开始搭建Wpf基础篇4-使用Prism进行模块化
    从零开始搭建Wpf初学篇3-界面模块化
    手写es5和es6实现原型继承
    判断类型(通用类型检测方法)和手写深拷贝
  • 原文地址:https://www.cnblogs.com/xkzy/p/3833852.html
Copyright © 2011-2022 走看看