当前位置:Gxlcms > JavaScript > javascript对象之间存在的三种继承方式

javascript对象之间存在的三种继承方式

时间:2021-07-01 10:21:17 帮助过:2人阅读

这个对象,不是那个对象,第三哦!

对象之间会存在继承,所以,来说一下他们之间存在的三种三种继承方式:

  1、冒用继承    

 1         //创建了孙悟空构造函数 
 2             function Sun(change,weapon,gf){ 
 3                 this.change = change; 
 4                 this.weapon = weapon; 
 5                 this.gf = gf; 
 6                 this.bianshen = function(){ 
 7                     alert("吃俺老孙一棒"); 
 8                 } 
 9             }
 10             
 11             //给原型上添加一个方法
 12             /*Sun.prototype.bianshen = function(){
 13                 alert("吃俺老孙一棒");
 14             }*/
 15             
 16             //创建猪八戒构造函数
 17             function Zhubajie(name){//name是猪八戒自己单独有的属性
 18                 this.name = name;
 19                 //创建一个属性,属性值是Sun构造函数
 20                 this.sun = Sun;
 21                 this.sun("仙桃","金箍棒","紫霞仙子");
 22                 delete this.sun;//删除掉这个属性
 23             }
 24             
 25             var zbj = new Zhubajie("猪八戒");
 26             
 27             
 28             zbj.bianshen();//方法可以使用
 29             alert(zbj.weapon);
 30             
 31             
 32             //zbj 和Sun  不是同一类型的
 33             alert(zbj instanceof Sun); //false

    这就是第一种继承方式。

    【注意】冒用继承缺点:不能使用原型上的方法和属性 优点:可以传递参数;

  2、原型继承

 1        function Dan(car,money,home){ 
 2                 this.car = car; 
 3                 this.money = money; 
 4                 this.home = home; 
 5             } 
 6              
 7             //发方法声明在原型对象上 
 8             Dan.prototype.shoping = function (){ 
 9                 alert("买买买");
 10             }
 11             Dan.prototype.money = "5亿";
 12             
 13             //这个实例化对象是Dan的儿子,可以使用Dan的属性和方法
 14             var son = new Dan("劳斯莱斯幻影","10亿","四合院");
 15             
 16             
 17             //创建一个乞丐构造函数
 18             function S(name){
 19                 this.name = name;
 20             }
 21             
 22             //让乞丐继承富豪的属性和方法
 23             //把乞丐的构造函数原型修改成干爹的原型,这样,乞丐实例化对象就可以使用干爹的属性和方法。(两种方法)
 24 //            S.prototype = Dan.prototype;
 25             S.prototype = new Dan();
 26             
 27             //把S的原型对象constructor指针指回自己,否则会出问题
 28             S.prototype.constructor = S;           
 29             var s = new S("苏乞儿");
 30             s.shoping();
 31             alert(s.money);
 32             
 33             //判断s的爸爸是不是  Dan
 34             alert(s instanceof Dan);//true  s 和  Dan不是一个东西

    这种继承方式就是将新建的父类对象赋给子类构造函数的原型。

    【注意】原型链继承缺点:不能传递参数 优点:可以使用原型 上的方法 ;    

  3、混合继承 

 1       function Person(name,id,sex){ 
 2                 this.name = name; 
 3                 this.id = id; 
 4                 this.sex = sex; 
 5                 this.think = function(ss){ 
 6                     alert(ss); 
 7                 } 
 8             } 
 9             
 10             Person.prototype.eat = function(){
 11                 alert("呵呵");
 12             }
 13             
 14             
 15             
 16             function XM(name,id,sex,clas){
 17                 this.clas = clas;
 18                 //call方法是用来继承用的。你想继承那个对性的属性,就要把属性传递进来;
 19 //                Person.call(this,name,id,sex);
 20                 
 21                 //apply和call功能相同;
 22                 //区别  call方法  参数要一个一个传,  apply方法可以传参数数组
 23                 //优先选择apply方法使用
 24                 Person.apply(this,arguments);
 25             }
 26             
 27             //原型链继承  +  call/apply   叫混合继承
 28             XM.prototype = new Person();
 29             XM.prototype.constructor = XM;
 30             
 31             
 32             var xiaoming = new XM("小明","12312112112332","男","一年级二班");
 33             alert(xiaoming.name);//打印小明名字属性
 34             xiaoming.think("坎坎坷坷");
 35             
 36             //现在有一个需要,让小明可以使用Person对象原型上的方法
 37             xiaoming.eat();

    第三种方式就是 冒用继承 + 原型继承 ,既能继承父类的原型,完成复用,又能向父类传递参数。

    javascript所有的内容就到这里了,希望可以帮到大家!!!!!!

以上就是javascript对象之间存在的三种继承方式的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行