8.1.2. Function Literals
JavaScript allows functions to be defined with function literals. As discussed in Chapter 3, a function literal is
an expression that defines an unnamed function. The syntax for a function
literal is much like that of the function statement, except that it is
used as an expression rather than as a statement and no function name is
required. The following two lines of code define two more or less identical
functions using a function statement and a function literal,
respectively:
function f(x) { return x*x; } // function statement
var f = function(x) { return x*x; }; // function literal
Although function literals create unnamed functions, the syntax allows a function name to be optionally
specified, which is useful when writing recursive functions that call
themselves. For example:
var f = function fact(x) { if (x <= 1) return 1; else return x*fact(x-1); };
This line of code defines an unnamed function and stores a
reference to it in the variable f. It does not store a reference to the function into a variable
named fact, but it
does allow the body of the function to refer to itself using that name. Note,
however, that this type of named function literal was not properly implemented
before JavaScript 1.5.
Because function literals are created by JavaScript expressions
rather than statements, they are quite flexible and are particularly well suited
for functions that are used only once and need not be named. For example, the
function specified by a function literal expression can be stored into a
variable, passed to another function, or even invoked directly:
f[0] = function(x) { return x*x; }; // Define a function and store it
a.sort(function(a,b){return a-b;}); // Define a function; pass it to another
var tensquared = (function(x) {return x*x;})(10); // Define and invoke
8.1.2 匿名函数
JavaScript允许函数声明为匿名函数,匿名函数是一个无名子的函数表达式,匿名函数的语法非常像函数的声明语句,当然,除了其是一个表达式而不是声明语句和没有函数名之外。以下两行代码声明了类似的同一函数,一个使用函数声明语句function,一个使用匿名函数
function f(x) { return x*x; } // function statement
var f = function(x) { return x*x; }; // function literal
虽然匿名函数创建了一个无名称的函数,但是语法允许可选择标示函数名,当写自调用的递归函数的时候,这个是非常有用的,例如:
var f = function fact(x) { if (x <= 1) return 1; else return x*fact(x-1); };
这行代码声明了一个匿名函数,并将该函数的引用存储在变量f中,它不能将函数引用存储在fact的变量中,但允许函数体
内使用fact名字调用其自身,注意,虽然如此,但匿名函数类型在JavaScript 1.5版本以前是不支持的
因为匿名函数是作为JavaScript表达式创建的而不是声明语句,所以他们是非常灵活的,尤其适合仅使用一次无需命名的场合,例如,匿名函数表达式可以存储在变量里,传给另外一个函数,或直接调用
f[0] = function(x) { return x*x; }; // Define a function and store it
a.sort(function(a,b){return a-b;}); // Define a function; pass it to another
var tensquared = (function(x) {return x*x;})(10); // Define and invoke
分享到:
相关推荐
《JavaScript: The Definitive Guide, 5th Edition》是JavaScript学习领域中一本经典的参考书,由David Flanagan撰写,全面且深入地介绍了JavaScript语言。这本书面向已经有一定编程基础的读者,旨在帮助他们掌握...
- **标题**: "[JavaScript权威指南(第6版)].(JavaScript:The Definitive Guide).David Flanagan.文字版.pdf" - **描述**: "[JavaScript权威指南(第6版)].(JavaScript:The Definitive Guide).David Flanagan.文字版...
This book is both an example-driven programmer's guide and a keep-on-your-desk reference, with new chapters that explain everything you need to know to get the most out of JavaScript, including:
Title: SSH, The Secure Shell: The Definitive Guide, 2nd Edition By: Daniel J. Barrett, Richard E. Silverman, Robert G. Byrnes Publisher: O'Reilly Media
With this digital Early Release edition of Hadoop: The Definitive Guide, you get the entire book bundle in its earliest form – the author’s raw and unedited content – so you can take advantage of ...
《JavaScript - The Definitive Guide 4th Edition》是一本权威的JavaScript编程指南,由David Flanagan撰写。这本书深入浅出地介绍了JavaScript的核心概念、语法和应用,是开发者必备的参考书籍之一。作为第四版,...
`Hadoop: The Definitive Guide`中可能会讲解如何创建、读取和操作HDFS上的文件,以及如何配置HDFS参数以优化性能。 MapReduce是Hadoop处理大数据的主要计算模型,它将大规模数据处理任务分解为小的“映射”和...
原名: JavaScript: The Definitive Guide: Activate Your Web Pages, 6th edition 作者: David Flanagan 版本: 英文文字版-pdf/EPUB + 完整书中源代码 出版社: O'Reilly 书号: 978-0596805524 发行时间: 2011年05月...
Wireless Networks: The Definitive Guide 这个比直接看802.11 协议要舒服一些。理解更方便。 802.11® Wireless Networks: The Definitive Guide By Matthew Gast Publisher : O'Reilly Pub Date : April 2002 ISBN...
Spark: The Definitive Guide: Big Data Processing Made Simple 1st Edition Spark: The Definitive Guide: Big Data Processing Made Simple 1st Edition Spark: The Definitive Guide: Big Data Processing Made ...
《Hadoop:The Definitive Guide》第二版是一本深入介绍Hadoop技术体系的权威书籍,由Tom White编写,Doug Cutting作序。本书旨在为读者提供一个全面且深入的理解Hadoop的技术背景、核心组件以及实际应用场景等内容...
Kafka the Definitive Guide Kafka 是一个分布式流媒体平台,用于构建实时数据处理和流媒体处理系统。下面是 Kafka 的一些重要知识点: 1. Kafka 概述 Kafka 是一个基于发布/订阅模式的消息队列系统,由 LinkedIn...
Hadoop: The Definitive Guide, 4th Edition Get ready to unlock the power of your data. With the fourth edition of this comprehensive guide, you’ll learn how to build and maintain reliable, scalable,...
HTML & XHTML: The Definitive Guide, 5th Edition is the most comprehensive, up-to-date book available on HTML and XHTML. The authors cover every element of HTML/XHTML in detail, explaining how each ...
- **书名**:《Hadoop:The Definitive Guide》(第二版) - **作者**:Tom White - **前言作者**:Doug Cutting - **出版社**:O'Reilly Media, Inc. - **出版日期**:2010年10月 - **版权**:版权所有 © 2011 Tom...
javascript the definitive guide 4th.rarjavascript the definitive guide 4th.rarjavascript the definitive guide 4th.rarjavascript the definitive guide 4th.rarjavascript the definitive guide 4th....
### HBase权威指南知识点概述 #### 一、引言与背景 - **大数据时代的来临**:随着互联网技术的发展,人类社会产生了前所未为的数据量。这些数据不仅数量巨大,而且种类繁多,传统的数据库系统难以应对这样的挑战。...