JavaScript中的arguments,callee,caller,call,appy

<script language="JavaScript">
/* *//*
 * 演示arguments的用法, 如何获取实参数和形数数
 */
function argTest(a, b, c, d){
   var numargs = arguments.length;
   // 获取被传递参数的数值。
   var expargs = argTest.length;
   // 获取期望参数的数值。
   alert("实参数目为:" + numargs)
   alert("形数数目为:" + expargs)

   alert(arguments[0])
   alert(argTest[0])          // undefined 没有这种用法
}
// argTest(1, 2)
// argTest(1, 2, 3, 4, 5)

/* *//*
 *  arguments不是数组(Array类)
 */

Array.prototype.selfvalue = 1;
function testAguments(){
   alert("arguments.selfvalue=" + arguments.selfvalue);
}
// alert("Array.sefvalue=" + new Array().selfvalue);
// testAguments();

 

 

/* *//*
 * 演示函数的caller属性.
 * 说明 : (当前函数).caller : 返回一个对函数的引用,该函数调用了当前函数
 */

function callerDemo() {
   if (callerDemo.caller) {
      var a = callerDemo.caller.arguments[0];
      alert(a);
   }
   else {
      alert("this is a top function");
   }
}
function handleCaller() {
   callerDemo();
}

// callerDemo();
// handleCaller("参数1", "参数2");


/* *//*
 * 演示函数的callee属性.
 * 说明 : arguments.callee : 初始值就是正被执行的 Function 对象, 用于匿名函数
 */
function calleeDemo() {
   alert(arguments.callee);
}
// calleeDemo();
// (function(arg0, arg1){alert("形数数目为:" + arguments.callee.length)})();


/* *//*
 * 演示apply, call函数的用法
 * 说明 : 作用都是将函数绑定到另外一个对象上去运行,两者仅在定义参数方式有所区别:
 *       apply(thisArg, argArray);
 *     call(thisArg[, arg1, arg2…] ]);
 *     即所有函数内部的this指针都会被赋值为thisArg
 */

function ObjectA(){
   alert("执行ObjectA()");
   alert(arguments[0]);
   this.hit = function(msg){
      alert(msg)
   }
   this.info = "我来自ObjectA"
}

function ObjectB(){
   alert("执行ObjectB()");
   // 调用ObjectA()方法, 同时ObjectA构造函数中的所有this就会被ObjectB中的this替代
   ObjectA.apply(this, arguments);
   // ObjectA.call(this);
   alert(this.info);
}
// ObjectB('参数0');


var value = "global 变量";
function Obj(){
   this.value = "对象!";
}
function Fun1(){
   alert(this.value);
}
// Fun1();
// Fun1.apply(window);
// Fun1.apply(new Obj());
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值