• js继承


    var animal=function(name){         //构造函数
        this.name=name;
        this.sayhello=function(){
    	alert("hi我是"+this.name);
        };
    }
    animal.prototype.shout=function(){     //prototype主要作用:给类增加一个新的属性或函数
        alert(this.name+"正在叫!");
    };
    animal.prototype.game=function(){
        alert(this.name+"正在玩耍!");
    };
    var dog=new animal("小黑");   //实例化
    dog.sayhello();
    dog.shout();
    dog.game();
    

    一、原型继承

    var animal=function(name){
        this.name=name;
        this.sayhello=function(){
    	alert("hi我是"+this.name);
        };
    }
    animal.prototype.shout=function(){
        alert(this.name+"正在叫!");
    };
    animal.prototype.game=function(){
        alert(this.name+"正在玩耍!");
    };
    var Dog=function(name){
        this.name=name;
        this.shout=function(){     //重写父类的函数
    	alert(this.name+"汪汪叫!");
        }	
    }
    Dog.prototype=Cat.prototype=new animal();
    
    var dog=new Dog("小黑");
    dog.sayhello();
    dog.shout();
    dog.game();
    var cat=new Cat("小白");
    cat.sayhello();
    cat.shout();
    cat.game();
    

    animal是父类(或超类),要继承于谁,谁就是父类(超类)

    改进:专门写个函数,用来实现继承

    var animal=function(name){
        this.name=name;
        this.sayhello=function(){
    	alert("hi我是"+this.name);
        };
    }
    Function.prototype.extends=function(superclass){           //extends是保留关键字,不能拿出来单独使用,如var extends=function(){},而这里可以使用是因为他做为函数的属性添加上去
        this.prototype=new superclass();
    };
    
    animal.prototype.shout=function(){
        alert(this.name+"正在叫!");
    };
    animal.prototype.game=function(){
        alert(this.name+"正在玩耍!");
    };
    var Dog=function(name){
        this.name=name;
        this.shout=function(){
    	alert(this.name+"汪汪叫!");
        }	
    }
    Dog.extends(animal);
    
    var dog=new Dog("小黑");
    dog.sayhello();
    dog.shout();
    dog.game();
    var dog=new Dog("小白");
    dog.sayhello();
    dog.shout();
    dog.game();
    

    二、call,apply继承(不完全继承)

    var animal=function(name){
        this.name=name;
        this.sayhello=function(){
    	alert("hi我是"+this.name);
        };
    }
    animal.prototype.shout=function(){
        alert(this.name+"正在叫!");
    };
    animal.prototype.game=function(){
        alert(this.name+"正在玩耍!");
    };
    var Dog=function(name){
        animal.call(this,name);	
    }
    var dog=new Dog("小黑");
    dog.sayhello();
    dog.shout();
    dog.game();
    

    输出:hi我是小黑

    TypeError: dog.shout is not a function

    通过call(apply)的方式,只能改变其this指向,通过prototype添加进去的方法,他是无法直接继承的

    总结:call,apply这种方式的继承适用于没有prototype的类或者不需要继承prototype所添加属性或函数的类,因为call和apply函数只是实现了方法的替换,而没有对对象的属性和函数进行复制操作

    原型继承,则可以继承所有的属性和函数

    继承的属性,只有删除原有属性,才会输出继承的属性

  • 相关阅读:
    dialogue中需要添加编辑器
    表格增加整行和删除整行
    树形菜单配合element-ui模糊搜索和鼠标单击选择内容变色并且滚动
    格式金钱【摘抄与网络,记录】
    element-ui日期选择器
    普通上传
    axios另类封装
    Android permission 访问权限大全
    Android Timer 的 schedule()方法定时循环切换图片
    Android 设置图片倒影效果
  • 原文地址:https://www.cnblogs.com/zhangwenkan/p/3956620.html
Copyright © 2020-2023  润新知