1.call 作用于的对象this是第一个参数,后面的不定长参数是作为参数传递过去的。
<input type="text" id="t" value="input value"/>
<script>
function aaa(g) {
alert(this.value);
alert(g);
}
aaa.call(document.getElementById('t') , 'test string');
</script>
2.apply 作用于的对象this是第一个参数, 第二个参数是一个数组,表示传递的所有参数
<script>
function sum() {
var s = 0;
for(var i = 0 ; i < arguments.length ; i++) {
s += arguments[i];
}
return s;
}
function aaa() {
return sum.apply(null , arguments);
}
var s = aaa(1,2,3,4,5,6,7,8,9,10);
alert(s);
</script>
3.arguments 表示函数调用过程中到结束之前,传递给函数的所有参数的集合, 类似于数组,但是不是数组,
使用数组的下标方法进行访问
arguments[0]表示第一个参数
arguments[1]表示第二个参数
arguments[2]表示第三个参数
4. arguments.callee 表示当前正在执行的函数,在无名函数的递归调用时能够派上用场。
<script>
alert((function(a) {
if(a == 0) {
return 1;
} else {
return a * arguments.callee(a - 1);
}
})(10));
</script>
5.arguments.callee.caller 表示当前正在执行的函数是由什么函数调用的,返回调用当前执行函数的函数的引用
<script>
function c() {
alert(arguments.callee.caller);
}
function b() {
alert(arguments.callee.caller);
c();
}
function a() {
alert(arguments.callee.caller);
b();
}
a();
</script>
6.arguments.caller 表示调用当前函数的函数的arguments对象。
<script>
function first() {
alert(arguments.caller.length);
}
function second() {
first();
}
second(1,2,3,4,5,6,7,8,9,10);
</script>
这个ie上有,ff上没有,太傻比了
7.函数定义的时候指定的参数
funname.length
<script>
function test(a,b,c,d,e,r) {
alert(arguments.callee.length);
alert(arguments.length);
}
test();
</script>
本文详细介绍了JavaScript中call、apply及arguments的相关用法,包括如何改变函数内部this指向及访问传递参数的方式。通过实例展示了不同场景下的应用技巧。
693

被折叠的 条评论
为什么被折叠?



