当前位置: 首页 > 图文教程 > 网络编程 > Javascript > JS教程:call、apply、callee用法

Javascript
Add a Table to a Word Document
Add Formatted Text to a Word Document
用jscript实现新建word文档
用jscript实现新建和保存一个word文档
Open and Print a Word Document
Use Word to Search for Files
Convert Seconds To Hours
Sample script that deletes a SQL Server database
Sample script that displays all of the users in a given SQL Server DB
firefox中用javascript实现鼠标位置的定位
div+css实现鼠标放上去,背景跟图片都会变化。
Locate a File Using a File Open Dialog Box
Save a File Using a File Save Dialog Box
用jscript实现列出安装的软件列表
List the Stored Procedures in a SQL Server database
Display SQL Server Login Mode
Display SQL Server Version Information
List all the Databases on a SQL Server
用jscript启动sqlserver
Stop SQL Server

Javascript 中的 JS教程:call、apply、callee用法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-28   浏览: 383 ::
收藏到网摘: n/a

可能不少学习javascript在使用call,apply,callee时会感到困惑,以下希望对于你有所帮助:

1、它是函数的方法或属性;
2、它可以改变执行上下文的this指向;
3、作为另一个对象调用一个方法(即可以把一个对象的方法作为另一个对象的方法来引用);
4、apply方法类似,但只能接收数组为参数;
5、callee函数的调用者。

f.call(o,1,2) 等同于
o.m = f;
o.m(1,2);

例1:
function o1(value){
if(value < 100){
this.value = value;
}else{
this.value = 100;
}
}

function o2(value){
o1.call(this,value);
alert(this.value);
}

var o = new o2(133554) //100  改变了this的指向

例2:
function c1(){
this.m1 = function(){
alert(this.name);
}
}

function c2(){
this.name = “mike”;
}
var nc1 = new c1();
var nc2 = new c2(); //必须
nc1.m1.call(nc2);  //mike 把方法m1作为对象nc2的方法来引用

例3:
function o1(arg){
if (arguments[1] < 100) {
this.value =
arguments[1] ;
}
else {
this.value = 100;
}
}

function o2(arg){
o1.apply(this, arg);
alert(this.value);
}

var o = new o2([101,60]) //60 参数只能是数组

callee用法,常用于匿名函数中
var factorial = function(x){
if(x <= 1){
return 1;
}
return x * arguments.callee(x - 1);
}
alert(factorial(5)); //120