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

How to write JavaScript regular function and arrow function

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

Share

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

This article mainly introduces the relevant knowledge of "how to write JavaScript routine function and arrow function". The editor shows you the operation process through an actual case. The operation method is simple and fast, and it is practical. I hope this article "how to write JavaScript routine function and arrow function" can help you solve the problem.

1-function declaration

This is what we know as a type, similar to the functionality of other programming languages. There are two types of function declarations:

A. traditional functions: traditional (regular) functions have the following modes:

Function functionName (optionalParameter/s) {} for example:

Function print () {console.log ("hello world");}

With parameters:

Function add (number1,number2) {return number1 + number2}

B. abbreviated function

An abbreviated function is a regular function, but for a class.. The difference in the pattern is that we don't write function keywords, so it is:

FunctionName (optionalParameter/s) {}

Let's see it in an example with a shorthand function, which we call login:

Class user: constructor (name,email) {this.userName = name; this.userEmail = email;} login () {console.log (`Hello ${this.name} you have login roomfully`);} 2-function expression

A function expression is like a function declaration, but we assign it to an object (variable) and let's discuss its four types:

A. regular function expression

Let's talk about what this means:

If we have this function:

Function print () {console.log ("hello world");}

Then when we want to call it, we type:

Print ()

But in regular function expressions, we save the function in the variable "assign the function to the variable", for example:

Let myPrint = function print () {console.log ("hello world");}

Now what happens if we try to call print? This will lead to an error that tells us that there is no print defined, so how can we get to the function? We can use the variable name assigned to the function (myPrint). In our example, we assign a function directly to the variable, which means that our variable comes from the function type, so let's try to call it, and the result is obviously valid.

b. Call the function expression (Immediately Invoked Function Expression) immediately

Judging from its name, IIFE will be called immediately, and you can think of it as a function calling itself and being executed. The grammar is very simple. We have a function: functionfunctionName () {Action,}

You can put parentheses or "parentheses" (functionfunctionName () {between actions,}) and end the function with another parenthesis in this way: (functionfunctionName () {Action}) ()

So what you expect now is that this function will be called immediately at the beginning of file execution

Let's write an example:

(function print () {console.log ("hello world");}) ()

Let's begin: let's execute it:

But wait... We said we were going to assign a function to a variable! Then let's do this:

Let myPrint = (function print () {console.log ("hello world");}) ()

What happens now? Let's use it:

The simple answer is that IIFE calls itself.. This means that when we assign a function to a variable, the function is called immediately and the saved content "assigned" to our myPrint variable is the result. So first we have to remove the call parentheses and try again: so now it works and the type of the variable is not defined

Let's change the function to give the variable myPrint a string type:

c. Anonymous function this is an anonymous function, that's all! It's just a function without a name..

The syntax is as follows: [var | let | const] variableName = function () {actions}

Note that the function here should be assigned to a variable, otherwise an error will be reported.

Additional example: let's try to combine anonymous functions with IIFE:

d. Arrowhead function

Here we are at last. Let's write the arrow function syntax step by step:

First: let's steal the anonymous function syntax [var | let | const] variableName = function () {actions} from above.

Next: delete the function keyword: [var | let | const] variableName = () {actions}

Finally, add the symbol (= >) after the parentheses: [var | let | const] variableName = () = > {actions}

It's done. This is our grammar, so let's try it now: our example:

Let myPrint = () = > {return "hello world";} this is the end of the introduction to "how to write JavaScript regular functions and arrow functions". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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