In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "javascript how to create functions", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to create functions in javascript" this article.
Creation method: 1, use the function keyword, syntax "function funName ([args]) {.}"; 2, use the Function () function, syntax "var funName=new Function (p1Magin p2Magne.pnPartibody)".
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
The method of creating function by javascript
1. Use the function keyword
Functions can be declared using the function keyword in JavaScript. The specific usage is as follows:
Function funName ([args]) {statements}
FunName is a function name, which, like a variable name, must be a legal identifier of JavaScript. After the function name is a list of parameters contained in parentheses, separated by commas. Parameters are optional and there is no limit to the number.
As identifiers, parameters are accessed only in the body of the function, and the parameters are private members of the function scope. When you call a function, you pass a value for the function, then use parameters to get the externally passed value, and interfere with the operation of the function inside the function.
After the parentheses is a curly bracket, and the statement contained in the braces is the main content of the function body structure. In the function weight, curly braces are essential, the lack of curly braces, JavaScript will throw syntax errors.
The function statement must contain function names, parentheses, and curly braces, and other code can be omitted, so the simplest function body is an empty function.
Function funName () {} / / empty function
If you use an anonymous function, you can omit the function name.
Function () {} / / Anonymous empty function
2. Use the Function () constructor
You can quickly generate a function using the Function () constructor. The specific usage is as follows:
Var funName = new Function (p1, p2,..., pn, body)
The parameter types of Function () are strings, p1~pn represents the list of parameter names of the created function, and body represents the function structure statement of the created function, separated by semicolons between the body statements.
You can omit all parameters and pass only a string to represent the function body.
Var f = new Function ("a", "b", "return aqb"); / / Clone the function structure through the constructor
In the above code, f is the name of the function you created. Functions are also defined, and functions with the same structure can be designed using function statements.
Function f (a, b) {/ / use the function statement to define the function structure return a + b;}
Use the Function () constructor to create an empty function structure without specifying any arguments.
Var f = new Function (); / / define an empty function
Use the Function () constructor to dynamically create a function that does not restrict the user to the function body pre-declared by the function statement. Using the Function () constructor allows you to use the function as an expression rather than as a structure, so it's more flexible to use. The disadvantage is that the Function () constructor is compiled at execution time and is very inefficient and is generally not recommended.
These are all the contents of the article "how to create functions in javascript". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.