zoukankan      html  css  js  c++  java
  • 对象冒充

    talk is cheap,show U the code!!

    //继承
    
    //1、对象冒充  可以支持多重继承
    
    //原理:构造函数使用this关键字,给所有的属性和方法赋值(即采用类声明的够着函数方式),例如
    function ClassA(color){
        this.color = color;
        this.sayColor = function(){
            console.log(this.color);
        }
    }
    function ClassB(color,name){
        //把ClassA作为常规函数来建立继承机制,而不是作为构造函数
        this.newMethod = ClassA;
        this.newMethod(color);
        delete this.newMethod;
    
        //所有的新属性和新方法都必须在删除了新方法的代码行后定义,
        //否则会覆盖超类的相关属性和方法
        this.name=name;
        this.sayName = function(){
            console.log(this.name);
        }
    }
    var objA=new ClassA('red'),
        objB=new ClassB('blue','Nicholas');
    objA.sayColor();   //red
    objB.sayColor();   //blue
    objB.sayName();     //Nicholas
    
    //call 方法 是与对象冒充方法最相似的方法
    function sayColor(prefix,suffix){
        console.log(prefix+this.color+suffix);
    }
    var obj=new Object();
    obj.color = 'red';
    
    //第一个参数是obj,说明应该赋予sayColor()函数中this关键字值是obj。
    sayColor.call(obj,'the color is ','a nice color'); //the color is reda nice color
    
    
    // 这里,原始的对象冒充 就可以改为:
    var ClassA=function (color){
        this.color = color;
        this.sayColor = function(){
            console.log(this.color);
        }
    };
    var ClassB=function(color,name){
    
        //把ClassA作为常规函数来建立继承机制,而不是作为构造函数
        ClassA.call(this,color);
    
        //所有的新属性和新方法都必须在删除了新方法的代码行后定义,
        //否则会覆盖超类的相关属性和方法
        this.name=name;
        this.sayName = function(){
            console.log(this.name);
        }
    };
    var objA=new ClassA('red'),
        objB=new ClassB('blue','Nicholas');
    objA.sayColor();   //red
    objB.sayColor();   //blue
    objB.sayName();     //Nicholas
    
    
    //apply 方法
    
    //call 方法 是与对象冒充方法最相似的方法
    function sayColor(prefix,suffix){
        console.log(prefix+this.color+suffix);
    }
    var obj=new Object();
    obj.color = 'red';
    
    //第一个参数是obj,说明应该赋予sayColor()函数中this关键字值是obj。
    sayColor.apply(obj,new Array('the color is ','a nice color')); //the color is reda nice color
    
    
    
    // 这里,原始的对象冒充 就可以改为:
    var ClassA=function (color){
        this.color = color;
        this.sayColor = function(){
            console.log(this.color);
        }
    };
    var ClassB=function(color,name){
    
        //把ClassA作为常规函数来建立继承机制,而不是作为构造函数
        ClassA.apply(this,[color]);
    
        //所有的新属性和新方法都必须在删除了新方法的代码行后定义,
        //否则会覆盖超类的相关属性和方法
        this.name=name;
        this.sayName = function(){
            console.log(this.name);
        }
    };
    var objA=new ClassA('red'),
        objB=new ClassB('blue','Nicholas');
    objA.sayColor();   //red
    objB.sayColor();   //blue
    objB.sayName();     //Nicholas
  • 相关阅读:
    Java学习路线:day1 Java语言概述
    Java学习路线:day5 Java基本语法(下)2
    Java学习路线:day4 Java基本语法(下)
    Python笔记_第四篇_高阶编程_GUI编程之Tkinter_2.控件类
    Python笔记_第四篇_高阶编程_GUI编程之Tkinter_1.使用Python进行GUI编程的概述
    Python笔记_第三篇_面向对象_9.Python中的"get"和"set"方法(@property和@.setter)
    Python笔记_第三篇_面向对象_8.对象属性和类属性及其动态添加属性和方法
    Python笔记_第三篇_面向对象_7.多态
    Python笔记_第三篇_面向对象_6.继承(单继承和多继承)
    Python笔记_第三篇_面向对象_5.一个关于类的实例(人开枪射击子弹)
  • 原文地址:https://www.cnblogs.com/web-fusheng/p/6823332.html
Copyright © 2011-2022 走看看