As one of the core technologies of JavaScript, function has a clear understanding of function mechanisms and usage, which is very important for Javascript development.
Today, I will introduce some simple usage of functions in Javascript based on my own practices.
1. What is a function?
From the definition of function in Javascript, function isCodeObject.
From this we can see that we can use functions as functions in C language, or object-oriented programming for functions. Of course, the function in Javascript is strong.
More than that.
2. How to Use Function 2.1 Definition
FunctionMyfunc (PARAM ){
//Code
}
Note that the two functions in JavaScript are considered to be the same:
Which function is called at runtime depends on the loading order, and the latter function will overwrite the previous one.
FunctionFunc1 (){Return'Function1 ';}
FunctionFunc1 (name ){ReturnName; }
In other words, function parameters are all optional parameters. Therefore, funciton recognition does not include parameters such as parameters. function input parameters are declared for reference convenience and readability.
The above code is also equivalent:
FunctionFunc1 (){
ReturnArguments [0] | 'function1 ';
}
Func ();//Return 'function1'
Func ('function ');//Return 'function'
2.2 function as an object
Yes. In JavaScript, function is an object. We can use function as an object.
It can have its own attributes and methods. There is a funciton as follows:
FunctionNameof (name ){
ReturnName. touppercase ();
}
2.2.1 function value assignment as an object
VaRPerson = person | {};
Person. nameof = nameof;
Person. nameof ('yang dong ')//Return "Yang Dong"
2.2.2 define Function Attributes
Let's take a look at the following code:
FunctionNameof (){ReturnNameof. Blogger ;}
Nameof. Blogger = "yang_dong ";
Yes, a function can have its own attributes.
Consider this scenario. If we want to count the number of times a function is called, there are two ways to achieve this:
1. Set a global variable to record the number of times the funciton is called. For each call, add 1 to the variable:
VaRGlobalnameofcounter = 0;
Nameof ();
Globalnameofcounter ++;
This seems to be okay. When the code is still relatively simple, it can work well, but as the Code becomes more complex, the cost of maintaining this logic will soar.
It is mainly because: globalnameofcounter pollutes the global namespace and destroys code encapsulation.
2. Use Function Attributes
Take a look at the following code:
function nameof () {
nameof. counter ++;
return nameof. blogger;
}
nameof. blogger = "yang_dong"
nameof. counter = 0;
nameof (); // nameof. counter = 1
nameof (); // nameof. counter = 2
Obviously, the second method has good encapsulation and maintainability. More applications with function attributes are available. See the following.
2.3 function as namespace
JavaScript does not support namespaces (I do not quite understand why such a powerful language does not support namespaces. Why ?),
However, we can still use its powerful funciton to support namespace.
From the above section, we know that a function can define its own attributes, and we can use this feature to generate a namespace. Please refer to the following code:
Nameof. getbloggername =Function(){
ReturnNameof. Blogger;
}
In this case, the nameof namespace already contains: blogger, counter attribute and function getbloggername.
2.4 function as Method
In JavaScript, function and method have no essential difference. If we do not distinguish them, I think this variable is different.
FunctionG (){Return This;}
VaRLocal = Local | {};
Local. method = g;//Modify this to point to local
Local. Method ();//Returns the local object.
G ();//Returns the domwindow object.
2.5 all functions are closure
All functions in JavaScript are bound with a scope chain, so it is a function that saves the call context. Let's take a look at the following instance code:
VaRVariable = 'global ';
FunctionGetvariable (){
VaRVariable = 'local ',
Func =Function(){
ReturnVariable;
};
ReturnFunc;
}
Getvariable ()();//Return local;
When a func is called, The varvisible value is a variable in the call context, not a global variable with the same name.
3. Summary
To summarize the introduction to funciton in one sentence, I think the function is a code set object that can be called and executed.
The above are some application scenarios of function, of course it is more than that.
example: function as a constructor (that is, funciton as a class), inheritance, etc. these content will be introduced to you in the future blog, so stay tuned.