javascript继承--call()和apply实现继承
2012-04-08 18:35
375 查看
call方法是Function对象中的方法,因此我们定义的每个函数都拥有该方法。可以通过函数名来调用call方法,call方法的第一个参数会被传递给函数中的this,从第2个参数开始,挨个赋值给函数中的参数
(2)
apply方法实现继承
function person(name){ alert(name+" : "+this.hobby); } var obj=new Object(); obj.hobby="打酱油"; person.call(obj,"小张");
(2)
<script language="javascript" type="text/javascript"> function Parent(name){ this.name=name; this.sayHello=function(){ alert(this.name); } } function Child(name,hobby){ this.hobby=hobby; Parent.call(this,name); this.say=function(){ alert(this.name+" : " +this.hobby); } } var ch=new Child("小明","打酱油"); ch.sayHello(); ch.say(); </script>
apply方法实现继承
function Parent(name){ this.name=name; this.sayHello=function(){ alert(this.name); } } function Child(name,hobby){ this.hobby=hobby; Parent.apply(this,[name]/* 或者new Array(name) */); this.say=function(){ alert(this.name+" : "+this.hobby); } } var ch=new Child("小明","打酱油"); ch.sayHello(); ch.say();
相关文章推荐
- javascript 中的继承实现, call,apply,prototype,构造函数
- JavaScript实现继承机制(2)——调用call()与apply()方法
- JavaScript中的call()和apply()方法,借此实现继承
- 基于JavaScript实现继承机制之调用call()与apply()的方法详解
- 【javascript】call或apply实现继承(多继承)
- JavaScript使用Call和Apply方法实现继承
- 基于JavaScript实现继承机制之调用call()与apply()的方法详解
- javascript 中用apply实现类的继承
- javaScript面向对象的方法实现继承:call方法
- JavaScript实现call、apply和bind
- 利用javascript中的call实现继承
- JavaScript call() 与 apply() 方法的实现与思路解析
- JS面向对象(3) -- Object类,静态属性,闭包,私有属性, call和apply的使用,继承的三种实现方法
- javascript使用call方式实现对象继承
- js中通过call()和apply()方法实现继承类
- js继承机制的实现apply or call
- Javascript中的apply和call继承
- JavaScript中 apply/call 与面向对象语言中继承关系的联系和区别
- javascript 的 apply实现继承
- 使用apply方法实现javascript中的对象继承