javacsript中call() 函数的认识
来源:互联网 发布:java验证码路径 编辑:程序博客网 时间:2024/05/18 03:24
call 方法
调用一个对象的一个方法,以另一个对象替换当前对象。
call([thisObj[,arg1[, arg2[, [,.argN]]]]])
参数1:thisObj
可选项。将被用作当前对象的对象。
参数2:arg1, arg2, , argN
可选项。将被传递方法参数序列。
说明:
call 方法可以用来代替另一个对象调用一个方法。call 方法可将一个函数的对象上下文从初始的上下文改变为由 thisObj 指定的新对象。
如果没有提供 thisObj 参数,那么 Global 对象被用作 thisObj。
看懂了没?说实话我没看懂,哈哈,没看懂继续往下看。
call 方法在js继承中经常看到,也一直搞不懂,这到底是什么东西,看下面这段代码:
function Person(name,age){ this.name = name; this.age=age; this.alertName = function(){ alert(this.name); } this.alertAge = function(){ alert(this.age); } } function webDever(name,age,sex){ Person.call(this,name,age); this.sex=sex; this.alertSex = function(){ alert(this.sex); } } var test= new webDever(“愚人码头”,28,”男”); test.alertName();//愚人码头 test.alertAge();//28 test.alertSex();//男
这样 webDever类就继承Person了,Person.call(this,name,age) 的 意思就是使用 Person对象代替this对象,那么 webDever中不就有Person的所有属性和方法了吗,test对象就能够直接调用Person的方法以及属性了;
说的再明白一点,就是相当于将webDever中的Person.call(this,name,age)这部分代码替换为Person类的:
[code=”javascript”]this.name = name; this.age=age; this.alertName = function(){ alert(this.name); } this.alertAge = function(){ alert(this.age); }[/code]
这样webDever类就相当于:
[code=”javascript”] function webDever(name,age,sex){ this.name = name; this.age=age; this.alertName = function(){ alert(this.name); } this.alertAge = function(){ alert(this.age); } this.sex=sex; this.alertSex = function(){ alert(this.sex); } } var test= new webDever(“愚人码头”,28,”男”); test.alertName();//愚人码头 test.alertAge();//28 test.alertSex();//男[/code]
不知道能不能这么理解?望大家斧正。
留一个问题:假设webDever类只要继承Person类中的alertName方法,这样可以吗?代码应该怎么写?
0 0
- javacsript中call() 函数的认识
- 关于js中call/apply的一点认识
- japplet到javacsript的 发展
- call 和 apply 的认识
- this call apply的认识
- javaScript 中 call 函数的用法说明
- javaScript 中 call 函数的用法说明
- javaScript 中 call 函数的用法说明
- javaScript 中 call 函数的用法说明
- js中call函数
- 对C++中函数模板的认识
- 对STL中函数对象的认识
- 对C++中函数对象的认识
- 对QT中connect函数的认识
- Makefile的call函数
- Makefile的call函数
- Makefile的call函数
- js的call函数
- Java回忆录(四)-------集合
- 求大整数的阶乘
- android 4.4 切换语言及解决切换语言黑屏的问题
- 二进制文件和文本文件的区别和联系
- 9.2 响应者链
- javacsript中call() 函数的认识
- 关于Testin崩溃接入会使崩溃率增加得问题
- IE下标签自动判断IE版本的语句<!--[if lte IE 6]> <![endif]-->
- ubuntu简繁体输入法快捷键转换
- Mac下老弹出mackeeper的网页
- android 设备唯一码的获取,Cpu号,Mac地址
- eclipse中Tomcat的配置
- 苹果的官方文档中提出的支持的动画
- Zend Studio 12.0.2设置文件定界符和文件编码(解决中文乱码问题)