javascript的几种继承方法介绍_

上传人:桔**** 文档编号:451358098 上传时间:2023-12-17 格式:DOCX 页数:10 大小:14.79KB
返回 下载 相关 举报
javascript的几种继承方法介绍__第1页
第1页 / 共10页
javascript的几种继承方法介绍__第2页
第2页 / 共10页
javascript的几种继承方法介绍__第3页
第3页 / 共10页
javascript的几种继承方法介绍__第4页
第4页 / 共10页
javascript的几种继承方法介绍__第5页
第5页 / 共10页
点击查看更多>>
资源描述

《javascript的几种继承方法介绍_》由会员分享,可在线阅读,更多相关《javascript的几种继承方法介绍_(10页珍藏版)》请在金锄头文库上搜索。

1、javascript的几种继承方法介绍_ 下面我就为大家带来一篇javascript的几种继承方法介绍。我觉得挺不错的。现在分享给大家,给大家一个参考 1.原型链继承:构造函数、原型和实例的关系:每个构造函数都有一个原型对象,原型对象都包含一个指向构造函数的指针,而实例都包含一个指向原型对象的内部指针。确认原型和实例之间的关系用instanceof。 原型链继承缺点:字面量重写原型会中断关系,用法引用类型的原型,并且子类型还无法给超类型传递参数 function Parent() this.name=mike; function Child() this.age=12; /儿子继承父亲(原型链

2、) Child.prototype=new Parent();/Child继承Parent,通过原型形成链条 var test=new Child(); console.log(test.age); console.log(test.name);/得到被继承的属性 /孙子连续原型链继承儿子 function Brother() this.weight=60; Brother.prototype=new Child();/继承原型链继承 var brother=new Brother(); console.log(brother.name);/继承了Parent和Child,弹出mike con

3、sole.log(brother.age);/12 console.log(brother instanceof Child);/ture console.log(brother instanceof Parent);/ture console.log(brother instanceof Object);/ture 2.构造函数实现继承:又叫伪造对象或经典继承。 构造函数实现继承缺点:借用构造函数虽然解决了原型链继承的两种问题,但没有原型,则复用无从谈起,所以需要原型链+借用构造函数模式。 function Parent(age) this.name=mike,jack,smith; thi

4、s.age=age; function Child(age) Parent.call(this,age);/把this指向Parent,同时还可以传递参数 var test=new Child(21); console.log(test.age);/21 console.log(test.name); test.name.push(bill); console.log(test.name);/mike,jack,smith,bill 3.组合继承:用法原型链实现对原型属性和方法的继承,而通过借用构造函数来实现对实例属性的继承。这样即通过在原型上定义方法实现了函数复用,又保证每个实现都有它自己的

5、属性。 缺点:无论什么状况下,都会调用两次超类型构造函数,一次是在创建子类型原型的时候,另一次是在创建子类型原型的时候,另一次是在子类型构造函数内部。 function Parent(age) this.name=mike,jack,smith; this.age=age; Parent.prototype.run=function() return this.name+ are both +this.age; function Child(age) Parent.call(this,age);/给超类型传参,其次次调用 Child.prototype=new Parent();/原型链继承,

6、第一次调用 var test1=new Child(21);/写new Parent(21)也行 console.log(test1.run();/mike,jack,smith are both 21 var test2=new Child(22); console.log(test2.age); console.log(test1.age); console.log(test2.run(); /这样可以使test1和test2分别拥有自己的属性age同时又可以有run方法 4.原型式继承:借助原型可以基于已有的对象创建新对象,同时还不必因此创建自定义类型。它要求必需有一个对象可以作为另一个

7、对象的基础。 function object(o) function F(); F.prototype=o; return new F(); var person= name:nicho, friends:shell,jim,lucy var anotherPerson = object(person); anotherPerson.name = Greg; anotherPerson.friends.push(Rob); console.log(anotherPerson.friends);/shell, jim, lucy, Rob var yetAnotherPerson = objec

8、t(person); yetAnotherPerson.name = Linda; yetAnotherPerson.friends.push(Barbie); console.log(yetAnotherPerson.friends);/shell, jim, lucy, Rob, Barbie console.log(person.friends);/shell, jim, lucy, Rob, Barbie ECMAScript5通过新增Object.create()方法规范化了原型式继承,这个方法接收两个参数:一个用作新对象原型的对象和(可选的)一个为新对象定义属性的对象。 var p

9、erson2= name:nicho, friends:shell,jim,lucy ; var anoP2=Object.create(person2); anoP2.name=Greg; anoP2.friends.push(Rob); console.log(anoP2.friends);/shell, jim, lucy, Rob var yetP2=Object.create(person2); yetP2.name=Linda; yetP2.friends.push(Barbie); console.log(yetP2.friends);/shell, jim, lucy, Rob

10、, Barbie console.log(person2.friends);/shell, jim, lucy, Rob, Barbie /*以这种方式指定的任何属性都会掩盖原型对象上的同名属性。*/ var threeP=Object.create(person, name:value:red ); console.log(threeP.name);/red,假如threeP中无name则输出person2里的name值nicho 5.寄生式继承:思路与寄生构造函数和工厂模式类似,即创建一个仅用于封装继承过程的函数,该函数在内部以某种方式来增加对象,最终再像真地是它做了全部工作一样返回对象。

11、 function object(o) function F(); F.prototype=o; return new F(); ; function createAnother(o) var cl=object(o); cl.sayHi=function() console.log(hi); return cl; ; var person= name:nick, friends:shelby,court,van var anotherPerson=createAnother(person); anotherPerson.sayHi();/hi console.log(anotherPerso

12、n.name);/nick console.log(anotherPerson.friends);/shelby, court, van /*这个例子中的代码基于 person 返回了一个新对象 anotherPerson 。 新对象不仅具有 person 的全部属性和方法,而且还有自己的 sayHi() 方法*/ 寄生组合式继承:无论什么状况下,都会调用两次超类型构造函数,一次是在创建子类型原型的时候,另一次是在创建子类型原型的时候,另一次是在子类型构造函数内部,这样子类型最终会包含超类型对象的全部实例属性,我们不得不在调用子类型构造函数时重写这些属性。因此消失了寄生组合式继承。 6.寄生组

13、合式继承:借用构造函数来继承属性,通过原型链的混成形式来继承方法。基本思路:不必为了指定子类型的原型而调用超类型的构造函数。本质上就是用法寄生式继承来继承超类型的原型,然后再将结果指定给子类型的原型。 function SuperType(name) this.name=name; this.colors=red,blue,green; SuperType.prototype.sayName=function() console.log(this.name); function SubType(name,age) SuperType.call(this,name); this.age=age; function object(o) function F(); F.prototype=o; return new F(); ; /*inheritPrototype此函数第一步是创建超类型原型的一个副本。其次步是为创建的副本添加constructor属性,

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 工作计划

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号