In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Today, I will talk to you about how to understand the recursive functions in JavaScript. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
What is a recursive function in the programming world, recursion is a means of calling itself, a recursive function: inside a function, calling itself, repeatedly.
/ / the following code is the simplest recursive function / / calls itself inside the function. As soon as the function is executed, it calls itself once, and executes again and again after the call. The loop does not stop function fn () {
Fn ()} fn ()
In fact, recursive functions are very similar to loops.
Need initialization, self-increment, code execution, condition judgment, otherwise it is an endless recursive function, which we call dead recursion.
Simply implement a recursion
Let's simply use recursive functions to achieve an effect.
Demand: find the sum of 1 to 5
Count 1 + 2 first and get 3.
Add 3 + 3 and you get 6.
Add 6 + 4 and you get 10.
If you count 10 + 5, you get 15.
End
To start writing, write the end condition first to write a recursive function (to avoid "dead recursion")
Function add (n) {
/ / pass in 1 / / end if when n = 5 (n = 5) {
Return 5
}}
Add (1)
Then write our recursive processing when the conditions are not met.
Function add (n) {
/ / pass in 1 / / end if when n = 5 (n = 5) {
Return 5
} else {
/ / when the condition is not met, it is the current number + the number return n + add (n + 1) that is 1 larger than you.
}} add (1)
Preview: get to know the object in advance
Object is a complex data type
Actually, it's complex, but it's not very complicated. It's just a collection of basic data types.
Var obj = {
Num: 100
Str: 'hello world'
Boo: true}
The {} here is different from the {} in the function.
The code in the function is written, and some data is written in the object.
An object is a collection of key-value pairs
Every key in {} is a member.
In other words, we can put some data in an object, so they don't interfere with each other.
In fact, we prepare a house, put the data we want in it, and then give the address of the house to the variable name. when we need a certain data, we can find the corresponding house according to the address stored in the variable name. then go to the house to find the corresponding data.
Create an object
Create an object literally
/ / create an empty object var obj = {}
/ / add a member to the image object obj.name = 'Jack'obj.age = 18
Create objects in the way of built-in constructors
/ / create an empty object var obj = new Object ()
/ / add a member to the object obj.name = 'Rose'obj.age = 20
Object is a built-in constructor that js gives us to create an object to use
After reading the above, do you have any further understanding of how to understand recursive functions in JavaScript? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
This article focuses on "how to use conditional comments to judge browsers". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use conditional comments to judge browsers!
Wechat
About us
Contact us
Product review
car news
thenatureplanet
More Form oMedia:
AutoTimes.
Bestcoffee.
SL News.
Jarebook.
Coffee Hunters.
Sundaily.
Modezone.
NNB.
Coffee.
Game News.
FrontStreet.
GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.