Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods of defining functions in JavaScript

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what are the methods of defining functions in JavaScript". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the methods of defining functions in JavaScript".

Method 1: function definition statement

Now let's take a look at the code:

/ / the summation function function sum (aheline b) {return aquib;}

The above code is our typical function declaration, by function followed by the function's name identifier, parentheses, and braces. For this kind of function definition, we need to display the specified function name. We can call our function through the function name during code execution. We can take a look at the following example:

Console.log (sum); / / the console outputs the source code of the sum function. At this time, the function has not yet defined function sum (aformab) {return aquib;} console.log (sum (2heli3)); / / 5

Since the function declaration has been introduced above, we have to say about the scope of the function. The function scope means that the function body of all variables declared in the function is always visible. This shows that our variable can be used before it is declared. Through this feature, we can call it declaration advance. Let's take a look at a string of code:

Var scope = "global"; function f () {console.log (scope); / / output "undefined" instead of "global" var scope = "local"; / / the variable is assigned an initial value here, but the variable itself is a defined console.log (scope) anywhere in the body of the function; / / output "local"} f (); the above code is equivalent to var scope = "global"; function f () {var scope / / declare a local variable at the top of the function, that is, declare console.log (scope) in advance; / / the variable exists and outputs "undefined" instead of "global" var scope = "local"; / / the variable assigns the initial value console.log (scope) here; / / outputs "local"} f ()

In the code, we declare all variables in the function body, and they are defined before the declaration, but they are assigned only when the variable is executed.

Method 2: function direct quantity expression

Let's take a look at the following code:

/ / the factorial function var factorial = function fact (x) {/ / assigns the function to a variable if (x)

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report