• javascript继承


     绑定构造函数


    在子类构造函数中使用Fatherconstructor.apply(this, arguments)

    eg:

    //父类
    function People(name,age){
        this.name = name;
        this.age = age;
        this.species = "human";
        this.getName = function(){
            return this.name;
        }
        this.getAge = function(){
            return this.age;
        }
        this.sayHello = function(){
            console.log("hi,I am the father class");
        }
    }
    
    //子类
    function Children(name,age){    
        People.apply(this,arguments)
    }
    
    var wish = new Children("wish");
    console.log("the name of wish is:"+wish.getName());  //the name of wish is:wish 
    console.log(wish.sayHello());  //hi,I am the father class 

    使用prototype


     详细见:js-prototype

    也使用prototype拷贝

    准备拷贝函数:

    function extend(Child, Parent){
        var parent = Parent.prototype;
        var child = Child.prototype;
        for(var i in parent){
            child[i] = parent[i];
        }
        child.uber = parent; //备用性质,指向上一层
    }

    eg:

    //父类
    function People(){
    }
    People.prototype.sayHello = function(){
        return "hello";
    }
    People.prototype.getName = function(){
        return this.name;
    }
    People.prototype.getAge = function(){
        return this.age;
    }
    
    //子类
    function Children(name, age){
        this.name = name;
        this.age = age;
    }
    
    //定义拷贝函数
    
    function extend(Child, Parent){
        var parent = Parent.prototype;
        var child = Child.prototype;
        for(var i in parent){
            child[i] = parent[i];
        }
        child.uber = parent; //备用性质,指向上一层
    }
    
    //子类
    
    extend(Children, People);
    var wish = new Children("wish","20");
    console.log(wish.getName());  //wish
    console.log(wish.getAge());  //20
    console.log(wish.sayHello());  //hello

     拷贝继承


    function deepCopy(p, c) {
        var c = c || {};
        for (var i in p) {
          if (typeof p[i] === 'object') {
            c[i] = (p[i].constructor === Array) ? [] : {};
            deepCopy(p[i], c[i]);
          } else {
             c[i] = p[i];
          }
        }
        return c;
    }

    详细见: js对象拷贝

    参考:http://www.ruanyifeng.com/blog

  • 相关阅读:
    SQL_Server_2005_数据类型转换函数(描述及实例)
    讨论:GUID与int自增列的问题
    SQL Server 2005无日志文件附加数据库
    优化SQL查询:如何写出高性能SQL语句
    开源项目之视频会议程序 Omnimeeting
    wzplayer player (android,windows,ios) 多核解码
    利用office2010 word2010生成目录
    利用office2010 word2010生成目录
    最近在忙活视频通话(sip)
    介绍几个在线画流程图的工具
  • 原文地址:https://www.cnblogs.com/wishyouhappy/p/3738232.html
Copyright © 2020-2023  润新知