Home > Web Front-end > JS Tutorial > body text

Analysis of the difference between callee and caller in javascript_javascript skills

WBOY
Release: 2016-05-16 16:03:13
Original
1121 people have browsed it

callee

callee is an attribute of the object, which is a pointer pointing to the function of the arguments object
First, let’s write an order function:

function chen(x){
if (x<=1) {
return 1;
} else{
return x*chen(x-1);
};
};
Copy after login

As can be seen from this function, a recursive function is used. If the function name is changed, the function name inside will also change. This is very inconvenient, so we use callee to try it

function chen(x){
if (x<=1) {return 1;
}else{
return x*arguments.callee(x-1);
};
};
Copy after login

Let’s analyze why it is written like this: According to the definition of callee, it can be seen that callee is an attribute of the arguments object and points to a function of the arguments object. This function is chen (chen=arguments.callee). This explanation should be understandable. Come on.

caller

caller is an attribute of the function object, which holds a reference to the function that calls the current function (pointing to the direct parent function of the current function)

Let’s take an example first

function a(){
b();
};
function b(){
alert(b.caller);
};
a(); //结果就是弹出函数a和内容
Copy after login

Let’s explain it. First, the attribute caller of function b calls the function reference a of the current function b (which points to the parent function a of the current function b), so the result is that function a(){ b();}; pops up;

Now that you understand caller and callee, can you combine the two to use them?

function b(){
alert(b.caller);
};
Copy after login

From this code, we can see that the b function name is called in the b function. This is very inconvenient when the function name changes. We need to replace the b
Previously we knew how to point to the current object, let’s modify it next:

(function a(){
b();
})();

function b(){
alert(arguments.callee.caller);
};

Copy after login

As can be seen from the code, we replaced the b function with arguments.callee, so the trouble was solved. . . . .

The above is the entire content of this article, I hope you all like it.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template