1. ホーム
  2. javascript

着信者及び発信者

2022-02-19 22:41:13

前回の記事で 引数 と、その属性の呼び出し側と、似たような外観の双子の兄弟呼び出し側である

まずはcalleeから、コードのスニペットで見てみましょう。

    var a=function(){
        console.log(arguments.callee);
    }
    var b=function(){
        a()
    }
    b();

このコードをクロームで表示すると、次のように出力されます。

ƒ (){
        console.log(arguments.callee);
    }

この関数はa関数と全く同じですか?

callee returns a reference to the function itself. callee is an attribute of arguments, which is a pointer to the function that has the arguments object (arguments is the calling function, so the callee is the calling function)

Let's start with an example of solving with a callee, the factorial calculation.





  function factorial(num){
        if(num<=1){

        }else{
            return num*factorial(num-1);
        }
    }
The code above is using the function name. The above method will enhance coupling, to reduce coupling we will use callee, and the recursive call can be done regardless of the name of the referenced function. Look at the callee.



   function factorial(num){
        if(num<=1){
            return 1;
        }else{
            return num*arguments.callee(num-1);
        }
    }

Similar to him we have a caller, which is normalized in es5 with another function object property caller; this property holds the function reference of the current function called, and returns null if the current function is called in the global scope
function outer(){
        inner();
    }
    function inner(){
1. alert(arguments.callee.caller);
  2. alert(inner.caller)
        // these two lines above are the same 1 is more loosely coupled than 2
     3. alert(arguments.caller);//undefined
// 3. This line of code will report an error in strict mode, but in non-strict mode it is undefined
    }
    outer();
// It says that the argumnets.callee property is defined to distinguish between qrguments.caller and caller
// You can't assign a value to the caller property in strict mode, otherwise you get an error
    // var a=function(){
    // alert(a.caller);
    // }// Define a function. Inside output a.caller
    // var b=function(){
    // a();
    // }// Define a function to call that a function.
    // b(); // output b function.
In the example above, it is the b called that knows the answer based on the properties of the caller.
    // var a=function(){
    // alert(a.caller);
    // }// Define a function. Inside output a.caller
    // var b=function(){
    // a();
    // }// Define a function to call that a function.
    // a(); // null (a is called in any function, that is, the top-level function, the output is null), the above example one, a function is called in b, so it is not the top-level does not regret null, return the current call is b function, and this example is called in the global natural is null
One last example to help provide insight into the top level: if there is a function in a, too


の呼び出し元となります。


var c=function(){
    alert(c.caller);
}
var a=function(){
    c()
    alert(a.caller);
}
var b=function(){
    a();
}
a();
The reason this example's alert comes out of the a function in c is because it's called in a.

The alert in a comes out null, after all you are calling the a function globally. the b function is equivalent to not using it.

Well, if you don't get it with all these examples, maybe I'm just a bad writer.





function outer(){
        inner();
    }
    function inner(){
1. alert(arguments.callee.caller);
  2. alert(inner.caller)
        // these two lines above are the same 1 is more loosely coupled than 2
     3. alert(arguments.caller);//undefined
// 3. This line of code will report an error in strict mode, but in non-strict mode it is undefined
    }
    outer();
// It says that the argumnets.callee property is defined to distinguish between qrguments.caller and caller
// You can't assign a value to the caller property in strict mode, otherwise you get an error

    // var a=function(){
    // alert(a.caller);
    // }// Define a function. Inside output a.caller
    // var b=function(){
    // a();
    // }// Define a function to call that a function.
    // b(); // output b function.

In the example above, it is the b called that knows the answer based on the properties of the caller.

    // var a=function(){
    // alert(a.caller);
    // }// Define a function. Inside output a.caller
    // var b=function(){
    // a();
    // }// Define a function to call that a function.
    // a(); // null (a is called in any function, that is, the top-level function, the output is null), the above example one, a function is called in b, so it is not the top-level does not regret null, return the current call is b function, and this example is called in the global natural is null

One last example to help provide insight into the top level: if there is a function in a, too
var c=function(){
    alert(c.caller);
}
var a=function(){
    c()
    alert(a.caller);
}
var b=function(){
    a();
}
a();

The reason this example's alert comes out of the a function in c is because it's called in a.
The alert in a comes out null, after all you are calling the a function globally. the b function is equivalent to not using it.

Well, if you don't get it with all these examples, maybe I'm just a bad writer.