Skip to Content
JavaScript 经典实例:第三版
book

JavaScript 经典实例:第三版

by Adam D. Scott, Matthew MacDonald, Shelley Powers
September 2022
Intermediate to advanced
585 pages
8h 17m
Chinese
China Electric Power Press Ltd.
Content preview from JavaScript 经典实例:第三版
函数
167
nestedGreeting('hello');
}
};
newObject.sayGreeting('hello');
};
如果不像这样赋值,第二个消息中的名称还是“
window
”,而不是“
object
”。
6.10
实现递归算法
6.10.1
问题
你想实现一个函数,调用自身完成一项任务。这种技术叫作递归,适用于处理具有
层次的数据结构(例如节点树或嵌套数组)、实现特定类型的算法(排序),以及
进行某些数学计算(斐波纳契数列)。
6.10.2
方案
递归是数学和计算机科学领域中一个广为人知的概念。数学中的斐波纳契数列就是
一例。在斐波纳契数列中,一个数等于前两个数之和。
f(n)= f(n-1) + f(n-2),
for n= 2,3,4,...,n and
f(0) = 0 and f(1) = 1
数学中的阶乘也是一种递归。阶乘通常使用感叹号表示(
4!
),指
1
到给定的数
n
之间所有整数之积。如果
n
4
,那么阶乘
4!
等于:
4! = 4 x 3 x 2 x 1 = 24
这些递归计算可以使用一系列循环和条件转换成
JavaScript
代码,不过也可以使用
函数递归实现。下面是一个递归函数,查找斐波纳契数列中的第
n
个数。
function
fibonacci(n) {
168
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

JavaScript

JavaScript

T. J. Crowder
JavaScript

JavaScript

David Flanagan
JavaScript Patterns

JavaScript Patterns

Stoyan Stefanov

Publisher Resources

ISBN: 9787519869601