zoukankan      html  css  js  c++  java
  • es5 简单继承

    var father=function(){};
    father.prototype={
        constructor:father,
        test:function(){
            console.log(1);
        }
    }
    var son=function(){};
    son.prototype={
        constructor:son,
        dd:function(){
            console.log(23);
        }
    };
    var tem=function(son){
        Object.getOwnPropertyNames(son.prototype).forEach(i=>{
            this[i]=son.prototype[i];
        });
    }
    tem.prototype=new father();
    tem.prototype.constructor=tem;
    son.prototype=new tem(son);
    son.prototype.constructor=son;
    var r=new son();
    console.log(r instanceof father);
    console.log(r instanceof son);

    使用了一个tem的function 然后将子类的prototype合并到实列本身 然后 r=new tem() r就是实例对象 然后 r实例具有 son的prototype属性就在r上面了 

  • 相关阅读:
    Ztree
    WebAPI
    数据传输
    jqyery+ajax 提交表单
    初试 MVC+Boostrap
    CSV 导入SQL Server 中
    10-C语言循环结构(一)
    Linux测试语句(test)
    C 链表
    C递归
  • 原文地址:https://www.cnblogs.com/me-data/p/9877681.html
Copyright © 2011-2022 走看看