Home > Web Front-end > JS Tutorial > body text

There are three inheritance methods between javascript objects

零下一度
Release: 2018-05-18 09:59:20
Original
1246 people have browsed it

This object, not that object, third!

There will be inheritance between objects, so let’s talk about the three inheritance methods between them:

 1. Pretending to use inheritance

 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
Copy after login

 This is The first inheritance method.

 【Note】Disadvantages of impersonating inheritance: You cannot use methods and properties on the prototype. Advantages: You can pass parameters;

 2. Prototypal inheritance

 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不是一个东西
Copy after login

 This inheritance method is Assign the newly created parent class object to the prototype of the child class constructor.

【Note】Disadvantages of prototype chain inheritance: parameters cannot be passed Advantages: methods on the prototype can be used;

3. Mixed inheritance

 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();
Copy after login

The third way is to use it fraudulently Inheritance + prototype inheritance can not only inherit the prototype of the parent class, complete reuse, but also pass parameters to the parent class.

That’s all the content of javascript, I hope it can help everyone! ! ! ! ! !

The above is the detailed content of There are three inheritance methods between javascript objects. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template