zoukankan      html  css  js  c++  java
  • 实现继承的方式

    /**
         * 借助构造函数实现继承
         */
        function Parent1(){
            this.name = "parent1";
        }
        Parent1.prototype.say = function(){};
        function Child1(){
            //将父构造函数的this指向子构造函数的实例上
            Parent1.call(this);//apply
            this.type = "child1";
        }
        console.log(new Child1);
    
        /**
         * 借助原型链实现继承
         */
        function Parent2(){
            this.name = 'parent2';
            this.play = [1,2,3];
        }
        function Child2(){
            this.type = 'child2';
        }
        Child2.prototype = new Parent2();
        console.log(new Child2);
    
        var s1 = new Child2();
        var s2 = new Child2();
        console.log(s1.play,s2.play);
        s1.play.push(4);
    
        /**
         * 组合方式
         * 缺点民:实例化两次父类
         */
        function Parent3(){
            this.name = "parent3";
            this.play = [1,2,3];
        }
        function Child3(){
            Parent3.call(this);
            this.type = 'child3';
        }
        Child3.prototype = new Parent3();
        var s3 = new Child3();
        var s4 = new Child3();
        s3.play.push(4);
        console.log(s3.play, s4.play);
    
        /**
         * 组合继承的优化1
         */
        function Parent4(){
            this.name = "parent4";
            this.play = [1,2,3];
        }
        function Child4(){
            Parent4.call(this);
            this.type = 'child4';
        }
        Child4.prototype = Parent4.prototype;
        var s5 = new Child4();
        var s6 = new Child4();
        console.log(s5, s6);
        console.log(s5 instanceof Child4,s5 instanceof Parent4);
        //构造函数是一个,无法判断实例是父类创建还是子类创建
        console.log(s5.constructor);
    
        /**
         * 组合继承优化2
         */
        function Parent5(){
            this.name = "parent5";
            this.play = [1,2,3];
        }
        function Child5(){
            Parent5.call(this);
            this.type = 'child5';
        }
        //创建中间对象
        Child5.prototype = Object.create(Parent5.prototype);
        Child5.prototype.constructor = Child5;
        var s7 = new Child5();
        console.log(s7 instanceof Child5, s7 instanceof Parent5);
        console.log(s7.constructor);
    //模拟new func就是一个构造函数
    	var new2 = function(func){
    		//第一步,创建一个空对象,空对象关联构造函数的原型对象
    		var o = Object.create(func.prototype);
    		//第二步,执行构造函数,k表示返回结果
    		var k = func.call(o);
    		//第三步,判断构造函数的动行结果是不是对象类型
    		if(typeof k === 'object'){
    			return k;
    		}else{
    			return o;
    		}
    	}
    

      

  • 相关阅读:
    Data Structure 之 KMC字符串匹配算法
    网站建站流程
    常用算法稳定性分析
    VSS错误:The Sourcesafe Web service cannot be accessed at the specified address
    Data Struture 之 指针
    Windows 之 CMD命令
    Data Structure 之 算法设计策略
    Data Structure 之 最优二叉树
    板级支持包(BSP)
    JPEG
  • 原文地址:https://www.cnblogs.com/huyanluanyu/p/10106248.html
Copyright © 2011-2022 走看看