• javascript 继承


    //对象工厂模式
    function createCar(color,doors,mpg){
        var tmpCar = new Object();
        tmpCar.color = color;
        tmpCar.doors = doors;
        tmpCar.mpg = mpg;
        tmpCar.showColor = function() { alert(this.color); };
        return tmpCar;
    }
    var car1 = createCar("red",4,20);
    var car2 = createCar("blue",2,15);
    car1.showColor();
    car2.showColor();
    //问题:这样写每个对象都有自己的showColor函数

    function showColor(){ alert(this.color); }
    function createCar(color,doors,mpg){
        var tmpCar = new Object();
        tmpCar.color = color;
        tmpCar.doors = doors;
        tmpCar.mpg = mpg;
        tmpCar.showColor = showColor;
        return tmpCar;
    }

    //用构造函数的方式,去掉函数内的临时变量
    function Car(color,doors,mpg){
        this.color = color;
        this.doors = doors;
        this.mpg = mpg;
        this.showColor = showColor;
    }
    //问题:showColor函数不像对象的方法

    //原型链
    function Car(color,doors,mpg){
        this.color = color;
        this.doors = doors;
        this.mpg = mpg;
    }
    Car.prototye.showColor = function() { alert(this.color); }

     //继承

    //原型链

    function Person(name){
        this.name=name;
    }
    Person.prototype.ShowName=function(){
        return "hello "+this.name;
    };
    Person.prototype.ShowPara=function(para){
        return "your para is:" + para;
    }

    function User(name,password){
        this.name=name;
        this.password=password;
    }
    User.prototype=new Person();
    User.prototype.ShowPwd=function(){
        return "your pwd: "+this.password;
    };

    var user=new User("terry",123456);
    alert(user.ShowName());
    alert(user.ShowPwd());
    alert(user.ShowPara("ookk"));

    //call方法

    function Person(name){
        this.name=name;
        this.ShowName=function(){
            return "hello "+this.name;
        };
    }
    Person.prototype.ShowPara=function(para){
        return "your para is:" + para;
    }

    function User(name,password){
        Person.call(this,name);
        this.name=name;
        this.password=password;
    }
    User.prototype.ShowPwd=function(){
        return "your pwd: "+this.password;
    };

    var user=new User("terry",123456);
    alert(user.ShowName());
    alert(user.ShowPwd());
    //alert(user.ShowPara("ookk"));  //错误,  call没有包括prototype扩展的方法

  • 相关阅读:
    Linux 下如何查看一个组内的有哪些用户
    Linux下查看用户列表
    用pureftpd+pureDB虚拟用户,建立一个简单安全(不需要数据库支持)的linux ftp站
    pure-ftp中如何设置匿名帐号,自定义匿名帐号访问指定目录
    PUTTY中永久更改字体大小
    Pure-ftpd 如何配置多用户,且A用户具有上传下载权限,B用户只有下载权限?
    windows10访问ftp中文乱码怎么办?
    WPF DynamicDataDisplay.dll 下载
    C# windows presentation foundation 项目中不支持Application
    c# NDP472-KB4054530-x86-x64-AllOS-CHS下载
  • 原文地址:https://www.cnblogs.com/vipcjob/p/1707358.html
Copyright © 2020-2023  润新知