Home >Web Front-end >JS Tutorial >A brief discussion on the internal properties of JavaScript functions_Basic knowledge

A brief discussion on the internal properties of JavaScript functions_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 16:18:45952browse

There are two special attributes inside the function: arguments and this. arguments is an array-like object containing all parameters passed in,
But this object also has an attribute called callee, which is a pointer to the function that owns the arguments object.

Please look at the classic factorial function example:

Copy code The code is as follows:

function Factorial(num) {
If (num <= 1) {
Return 1;
              } else {
Return num * Factorial(num - 1);
            }
}
function Factorial(num) {
If (num <= 1) {
Return 1;
              } else {
Return num * arguments.callee(num - 1);
            }
}

There is nothing wrong with using the first method, but the coupling is too high, which is not good. After the function name is changed, the internal function name must also change
The second method is low coupling. No matter how the function name changes, it will not affect the function execution.

This refers to the environment object in which the function data is executed, or it can also be said to be the this value

Copy code The code is as follows:

          window.color = "red";
      var o = {color: "blue"};
function sayColor() {
alert(this.color);
}
         sayColor();//red
o.sayColor = sayColor;
o.sayColor();//blue

The caller attribute holds the reference of the function that calls the current function. If the current function is called in the global scope, its value is Null

Copy code The code is as follows:

function outer() {
innter();
}
         function innter(){
                   //alert(innter.caller);//The coupling is too high
alert(arguments.callee.caller);
}
        outer();

The above is all the content of the internal properties of javascript functions. I hope you guys like it

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