In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "what are the types of javascript definition functions", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "what are the types of javascript definition functions" this article.
Define several types of functions in JavaScript: 1, use the function keyword to define the function through a declaration or expression, the syntax is "function functionName (parameters) {executed code}"; 2, use the function constructor Function () to construct the function.
The operating environment of this tutorial: windows10 system, javascript1.8.5 version, Dell G3 computer.
What are several types of functions defined by javascript
JavaScript uses the keyword function to define functions.
A function can be defined by a declaration or an expression.
Function declaration
In the previous tutorial, you learned about the syntax of function declarations:
Function functionName (parameters) {Code executed}
The function will not be executed immediately after it is declared, but will be called when we need it.
Function constructor
The function constructor is used to create a function object, and virtually every function in JavaScript is a function object.
The syntax of the function constructor:
New Function ([arg1 [, arg2 [,... argN]],] functionBody)
Arg1, arg2,... ArgN is used as the variable name of the normal parameter name of the function, which must be a string that conforms to the JavaScript variable identification specification to represent a single variable or a comma-separated string that represents multiple variables, such as "x", "theValue", and or "aline b".
FunctionBody contains a string of JavaScript statements defined by the function.
Description
1. A function object is created when the function constructor is parsed into a function. But this is less efficient than using function declarations, function expressions, and so on, because these functions are parsed directly by code.
two。 The string parameters passed to the function constructor are treated as the variable parameter names of the function generated by the function constructor, in the order in which they appear as the parameters of the generated function.
3. Calling the function constructor in the form of a function (which doesn't work or modified with the new keyword) has the same effect.
Examples
/ / Example can be run directly in your JavaScript console// Create a function that takes two arguments and returns the sum of those argumentsvar adder = new Function ('await,' baked, 'return a + b'); / / Call the functionadder (2,6); / / > 8
Parameters an and b are normal parameter names that can be used inside the function body.
The generated function is:
Function (aformab) {undefined return aforb;}
A way to parse json: var json = (new Function ("return" + str)) ()
The principle is the same
The generated function is:
Function toJson (str) {undefined return str;} var str= "{'aqiaqizhuo 1"; toJson (str)
That is, function () {return {'axiaqizhuo 1jinghuanghuanghuanghuanghuanghuanghuanghuanfangbang (abc'}}).
The difference between function constructors and function declarations:
Functions created with function constructors do not create closures in context, they are always created in global scope, and when executing the created function, they can only use their own local or global variables, unlike eval.
The above is all the content of the article "what are the types of functions defined by 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.