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 经典实例:第三版
140
6
函数
函数是离散、可重用的代码单元,是程序的基本组成构件。但是在
JavaScript
中,
这只说对了一部分。
JavaScript
函数其实也是对象,是
Function
类型的实例,可以赋值给变量,也可以
当作值在代码中传递。
JavaScript
函数可以没有名称,直接在表达式中声明,或者使
用箭头句法进一步简化。一个函数甚至可以放在另一个函数中,把函数的状态限制
在一定的范围内(这叫闭包)。
函数还是
JavaScript
面向对象编程的核心概念。这是因为,自定义的类其实就是一
种特殊类型的构造函数(详见第
8
章)。在
JavaScript
中,一切最终都可以归结为
函数。
6.1
把一个函数作为参数传给另一个函数
6.1.1
问题
你调用的函数接受一个函数为参数。传入这样一个参数,采用什么方式最好呢?
6.1.2
方案
JavaScript
中,很多函数接受甚至要求传入函数作为参数。有些操作需要在任务
函数
141
完成后触发一个回调函数,还有些操作则使用函数完成其他任务。例如,
Array
象的许多方法要求提供一个函数,用于排序、转换、组合或选择数据。数组多次使
用你提供的函数,处理每一个元素。
提供函数作为参数的方式有很多,下面三种最常用。
引用在代码中的某个地方声明的函数。如果你还想在应用的其他部分使用这个 ...
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 Patterns

JavaScript Patterns

Stoyan Stefanov
JavaScript

JavaScript

David Flanagan
JavaScript

JavaScript

T. J. Crowder

Publisher Resources

ISBN: 9787519869601