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

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-28   浏览: 376 ::
收藏到网摘: 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