Home  >  Article  >  Web Front-end  >  js arguments,jcallee caller usage summary

js arguments,jcallee caller usage summary

高洛峰
高洛峰Original
2017-01-04 17:21:591183browse

Keywords: arguments, callee, caller
arguments: Represents the parameters passed into the function
callee: Represents the statement of the function and the function body
caller: Represents the function that calls the function

arguments

This object represents the parameters of the function being executed and the function that calls it.

caller

Returns a reference to the function that called the current function.
functionName.caller
functionName object is the name of the executed function.

Explanation
For functions, the caller attribute is only defined when the function is executed. If the function is called from the top level, caller contains null . If the caller attribute is used in a string context, the result is the same as functionName.toString, that is, the decompiled text of the function is displayed.

callee

Returns the Function object being executed, which is the body of the specified Function object.

[function.]arguments.callee

Optional function parameter is the name of the Function object currently being executed.

Explanation

The initial value of the callee attribute is the Function object being executed.

The callee attribute is a member of the arguments object. It represents a reference to the function object itself. This is beneficial to the recursion of anonymous functions or to ensure the encapsulation of functions. For example, the following example recursively calculates the natural numbers from 1 to n. and. This property is only available when the related function is executing. It should also be noted that callee has a length attribute, which is sometimes better for verification. arguments.length is the actual parameter length, and arguments.callee.length is the formal parameter length. From this, you can determine whether the formal parameter length is consistent with the actual parameter length when calling.

<script type=&#39;text/javascript&#39;>
function test(x,y,z) 
{ 
alert("实参长度:"+arguments.length);
alert("形参长度:"+arguments.callee.length);
alert("形参长度:"+test.length);
alert(arguments[ 0 ])         
alert(test[ 0 ])           // undefined 没有这种用法
}
//test(1,2,3); 
test(1,2,3,4);
/*
*  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(&#39;参数0&#39;);

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


For more js arguments, jcallee caller usage summary and related articles, please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn