js--继承
2016-04-17 16:07
555 查看
js继承一般分为3种办法,一种是es5基于原型的继承以及es5的call( ),一种是es6基于类继承
// 使用JavaScript继承机制,实现两个类之间的继承。
// 父类:人,属性:年龄、身高、性别。方法:行走、行走、睡觉。
// 子类:学生,属性:年级、学分。方法:上学、放学、休学。
// 目标,创建子类学生的实例,调用行走、吃饭、睡觉、上学、放学、休学6个方法。
【基于原型继承】
【call( )、apply( )】
说简单一点,这两函数的作用其实就是更改对象的内部指针,即改变对象的this指向的内容。这在面向对象的js编程过程中有时是很有用的。
两个函数基本相同,唯一的区别是,后者可以接受多个参数,即参数可以数组
func.call(func1,var1)对应的apply写法为:func.apply(func1,[var1,var2,var3])。
【基于类继承】
// 使用JavaScript继承机制,实现两个类之间的继承。
// 父类:人,属性:年龄、身高、性别。方法:行走、行走、睡觉。
// 子类:学生,属性:年级、学分。方法:上学、放学、休学。
// 目标,创建子类学生的实例,调用行走、吃饭、睡觉、上学、放学、休学6个方法。
【基于原型继承】
//es5实现继承[基于原型继承] function Person(age,height,sex){ this.name=name; this.age=age; this.height=height; this.sex=sex; this.go=function(){ console.log("行走") }; this.eat=function(){ console.log("行走") }; this.sleep=function(){ console.log("睡觉") }; } function Student(nj,sc){ this.nj=nj; this.sc=sc; this.goSchool=function(){ console.log("上学") } this.bybySchool=function(){ console.log("放学") } this.overSchool=function(){ console.log("休学") } } Student.prototype=new Person(); var stu=new Student(2,30); stu.go(); stu.sleep(); stu.goSchool(); stu.bybySchool(); stu.overSchool();
【call( )、apply( )】
说简单一点,这两函数的作用其实就是更改对象的内部指针,即改变对象的this指向的内容。这在面向对象的js编程过程中有时是很有用的。
两个函数基本相同,唯一的区别是,后者可以接受多个参数,即参数可以数组
func.call(func1,var1)对应的apply写法为:func.apply(func1,[var1,var2,var3])。
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> </head> <body> <script type="text/javascript"> //类式继承:利用JS中的call()和apply()方法 function Animal(){ this.name = "Animal"; this.showName = function(){alert(this.name);} } function Cat(){this.name = "Cat";} var animal = new Animal(); var cat = new Cat(); //通过call或apply方法,将原本属于Animal对象的showName()方法交给对象cat来使用了。 animal.showName.call(cat,","); //animal.showName.apply(cat,[]); </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> </head> <body> <script type="text/javascript"> function Animal(name){ this.name = name; this.showName = function(){ alert(this.name); } } function Cat(name){ Animal.call(this, name); } var cat = new Cat("Black Cat"); cat.showName(); /*Animal.call(this) 的意思就是使用 Animal对象代替this对象, * 那么 Cat中不就有Animal的所有属性和方法了吗, * Cat对象就能够直接调用Animal的方法以及属性了.*/ </script> </body> </html>
【基于类继承】
//es6实现继承[基于类继承] class Person{ constructor(age,height,sex){ this.age=age; this.height=height; this.sex=sex; } go(){ console.log("行走") }; eat(){ console.log("行走") }; sleep(){ console.log("睡觉") }; } class Student extends Person{ constructor(nj,sc){ super() this.nj=nj; this.sc=sc; } goSchool(){ console.log("上学") } bybySchool(){ console.log("放学") } overSchool(){ console.log("休学") } } var stu=new Student(2,30); stu.go(); stu.sleep(); stu.goSchool(); stu.bybySchool(); stu.overSchool();
相关文章推荐
- JS学习3(变量、作用域和内存)
- 【 D3.js 入门系列 --- 2 】 怎样使用数据和选择元素
- 关于js对象属性
- 堆优化 Dijstra单源最短路径算法 2(邻接表)
- js keyup、keypress和keydown事件 详解
- js笔记
- JS区分浏览器页面是刷新还是关闭
- Json----Jackson 下载地址
- javascript VS java
- js组件SlotMachine实现图片切换效果制作抽奖系统
- JSP原理和语法
- move.js
- jsp页面中的EL表达式不被解析的问题
- js实现鼠标监听
- JS中取二维数组中最大值的方法汇总
- 最简单js代码实现select二级联动下拉菜单
- JS正则表达式——学习笔记
- JavaScript专业规则12条
- JS基础回顾(二)--数组
- 使用Jsoup对豆瓣读书进行爬虫