js函数表达式与函数声明

科技术语系列 (Tech Jargon Series)

It’s likely you already know how to write functions in both these ways. function doStuff() {} and () => {} are characters we type all day. But how are they different and why use one over the other?

您可能已经知道如何以这两种方式编写函数。 function doStuff() {}() => {}是我们整天键入的字符。 但是它们有何不同?为什么要在另一个之上使用?

Note: Examples are given in JavaScript. Your Mileage May Vary with other languages.

注意:示例在JavaScript中给出。 Ÿ我们的并购 ileage 中号 AY V进制与其他语言。

第一个区别:名字 (The first difference: a name)

When you create a function with a name, that is a function declaration. The name may be omitted in function expressions, making that function “anonymous”.

使用名称创建函数时,即为函数声明 。 该名称可以在函数表达式中省略,从而使该函数“匿名”。

Function declaration:

函数声明:

function doStuff() {};

Function expression:

函数表达式:

const doStuff = function() {}

We often see anonymous functions used with ES6 syntax like so:

我们经常看到与ES6语法一起使用的匿名函数,如下所示:

const doStuff = () => {}

吊装 (Hoisting)

Hoisting refers to the availability of functions and variables “at the top” of your code, as opposed to only after they are created. The objects are initialized at compile time and available anywhere in your file.

提升是指函数和变量在代码“顶部”的可用性,而不是仅在它们创建之后。 这些对象在编译时初始化,并且可以在文件中的任何位置使用。

Function declarations are hoisted but function expressions are not.

函数声明被悬挂,但函数表达式未被悬挂。

It’s easy to understand with an example:

用一个例子很容易理解:

doStuff();
function doStuff() {};

The above does not throw an error, but this would:

上面没有抛出错误,但是会:

doStuff();
const doStuff = () => {};

函数表达式的情况 (The case for function expressions)

It might seem like function declarations, with their powerful hoisting properties, are going to edge out function expressions for usefulness. But choosing one over the other requires thinking about when and where the function is needed. Basically, who needs to know about it?

函数声明及其强大的提升属性似乎会使函数表达式失去实用性。 但是要选择一个,则需要考虑何时何地需要该功能 。 基本上,谁需要知道?

Function expressions are invoked to avoid polluting the global scope. Instead of your program being aware of many different functions, when you keep them anonymous, they are used and forgotten immediately.

调用函数表达式可以避免污染全局范围 。 当您使它们保持匿名状态时,它们会立即被使用和忘记,而不是您的程序意识到许多不同的功能。

国际教育展 (IIFE)

The name — immediately invoked function expressions — pretty much says it all here. When a function is created at the same time it is called, you can use an IIFE, which looks like this:

名称- 立即调用的函数表达式 -在这里几乎说明了一切。 在同时调用一个函数时,可以使用如下所示的IIFE:

(function() => {})()

or:

要么:

(() => {})()

For an in-depth look at IIFEs, check out this comprehensive article.

要深入了解IIFE,请查看这篇综合文章 。

回呼 (Callbacks)

A function passed to another function is often referred to as a “callback” in JavaScript. Here’s an example:

传递给另一个函数的函数在JavaScript中通常称为“回调”。 这是一个例子:

function mapAction(item) {// do stuff to an item
}
array.map(mapAction)
array.map(mapAction)

The problem here is that mapAction will be available to your entire application — there’s no need for that. If that callback is a function expression, it will not be available outside of the function that uses it:

这里的问题是mapAction将可用于您的整个应用程序- mapAction 。 如果该回调是函数表达式,则在使用它的函数之外将无法使用它:

array.map(item => { //do stuff to an item })

or

要么

const mapAction = function(item) {// do stuff to an item
}
array.map(mapAction)
array.map(mapAction)

though mapAction will be available to code below its initialization.

尽管mapAction将可用于其初始化下方的代码。

摘要 (Summary)

In short, use function declarations when you want to create a function on the global scope and make it available throughout your code. Use function expressions to limit where the function is available, keep your global scope light, and maintain clean syntax.

简而言之,当您要在全局范围内创建函数并使之在整个代码中可用时,请使用函数声明。 使用函数表达式可限制该函数在何处可用,使全局作用域保持清淡并维护简洁的语法。

参考文献 (References)

  • Function declaration, MDN docs.

    函数声明 ,MDN文档。

  • Function expression, MDN docs.

    函数表达式 ,MDN文档。

  • Hoisting, MDN glossary.

    吊装 ,MDN术语表。

科技术语系列 (The Tech Jargon Series)

There are so many phrases that get thrown around at tech meetups and conferences, assuming that everyone is already down with the lingo. I’m often not down with the lingo. It’s common for developers to act astonished that I lack a piece of knowledge.

假设每个人都已经对这种术语感到沮丧,那么在技术聚会和会议上会出现很多短语。 我常常不喜欢行话。 开发人员惊讶地发现我缺乏知识,这很常见。

The truth is, I often just don’t know the right word for it. As humans, but especially developer humans, we love to dismiss those who don’t “talk the talk”, so this series is about getting a solid understanding of programming concepts that one “should know”.

事实是,我常常只是不知道正确的词。 作为人类,尤其是开发人员,我们喜欢解雇那些不“谈论”的人,因此本系列文章旨在对人们“应该知道”的编程概念有扎实的理解。

This is the second article in the series. The first was higher-order functions. Look out for more as I go to meetups and conferences and pretend to know what my fellow techies are talking about, but then have to go home and Google it.

这是该系列的第二篇文章。 首先是高阶函数 。 当我去参加聚会和会议时要注意更多,并假装知道其他技术人员在谈论什么,但随后必须回家并使用Google。

翻译自: https://www.freecodecamp.org/news/when-to-use-a-function-declarations-vs-a-function-expression-70f15152a0a0/

js函数表达式与函数声明

js函数表达式与函数声明_何时使用函数声明与函数表达式相关推荐

  1. 函数c语言画箭头_什么时候不使用箭头函数

    我自己是一名从事了多年开发的web前端老程序员,目前辞职在做自己的web前端私人定制课程,今年年初我花了一个月整理了一份最适合2019年学习的web前端学习干货,各种框架都有整理,送给每一位前端小伙伴 ...

  2. c语言程序设计函数6,C语言程序设计_哈工大(6):函数.pdf

    圳 职 业 技 术 学 院Shenzhen Polytechnic 六单元 :函数 教学内容 函数 教学目标 应知 了解模块化程序的结构 掌握函数的定义方法 掌握函数的调用和参数传递 了解预处理 重点 ...

  3. python函数能否增强代码可读性_总结的几个Python函数方法设计原则

    在任何编程语言中,函数的应用主要出于以下两种情况: 1.代码块重复,这时候必须考虑用到函数,降低程序的冗余度 2.代码块复杂,这时候可以考虑用到函数,增强程序的可读性 当流程足够繁杂时,就要考虑函数, ...

  4. python函数的使用方法图解_零基础python之4函数重用-函数与模块(附详细的步骤和程序)...

    4代码重用--函数与模块 重用代码是构建一个可维护系统的关键. 代码组是Python中对块的叫法. 对之前的vowels代码功能创建一个函数名为search_for_vowels( ) 在函数调用时结 ...

  5. c语言自定义char*函数返回值是乱码_[每日C语言」printf()函数的修饰符和返回值...

    在上一个小demo<printf()函数(1)>中主要说了一下printf()函数的转换说明符,这些转移说明符是可以被修饰的.我们可以在%d和定义的转义字符之间通过插入修饰符对基本的转换说 ...

  6. pta函数统计素数并求和_关于求和的4种函数公式,此文讲透了,尤其是第4种,绝对的高效...

    求和,谁都可以做,基本都会做--基本都会做?Why?为什么不是全部会做?那是因为很多亲遇到附加条件求和的时候就犯难-- 一.Sum函数:求和.目的:统计总销量.方法:1.在目标单元格中输入公式:=SU ...

  7. range函数python2和3区别_【后端开发】range函数python2和3区别

    range函数是一个用来创建算数级数序列的通用函数,返回一个[start, start + step, start + 2 * step, ...]结构的整数序列: py2中的range()函数用法: ...

  8. mysql的count函数类型是什么意思_详细解读MySQL中COUNT函数的用法

    MySQL的COUNT函数是最简单的功能,非常有用的计算,预计由一个SELECT语句返回的记录数. 要了解COUNT函数考虑的EMPLOYEE_TBL的的表具有以下记录: mysql> SELE ...

  9. 在python中函数不可以嵌套调用_在Python中调用嵌套函数

    我有一个方法,我已经分解成更小的嵌套函数来分解代码基:def foo(x,y): def do_this(x,y): pass def do_that(x,y): pass do_this(x,y) ...

  10. java表达式由什么组成_必知必会之Lambda表达式

    Java是一门强大的面向对象的语言,除了8种基本的数据类型,其他一切皆为对象.因此,在Java中定义函数或方法都离不开对象,也就意味着很难直接将方法或函数像参数一样传递,而Java8中的Lambda表 ...

最新文章

  1. 205页PPT,看5G+AI引领的下一个时代!
  2. 力扣(LeetCode)刷题
  3. java线程占用CPU_在windows下揪出java程序占用cpu很高的线程并完美解决
  4. selenide 自动化测试进阶一: 查找元素和相关操作
  5. Unable to update index for central http://repo1.maven.org/maven2/
  6. OMEGA3-补充注意事项
  7. java基础第五篇封装与面向对象
  8. Codeforces 439C Devu and Partitioning of the Array(模拟)
  9. 浪潮之巅-读书笔记一
  10. 网络系统设计的一般步骤
  11. 4个方法,教你1分钟查询你的手机注册了多少软件和网站
  12. 由内而外全面进化,影像娱乐都出彩,vivo S12 Pro上手
  13. 使用Python给罗永浩生成卡通头像
  14. 安装使用 apt-cyg
  15. 傅里叶级数构建信号要求频率有正有负_电子科大学长说—信号与系统考研例题详解重点习题...
  16. linux安装glib,glib源码安装使用方法
  17. 这十个时间千万别受孕
  18. 全文索引elasticsearch
  19. java编程找出吸血鬼数字,Java 找到四位数的所有吸血鬼数字 基础代码实例
  20. LINUX ROUTE命令详解-2

热门文章

  1. word中图片为嵌入式格式时显示不全_word嵌入图片显示不全,教您word插入图片显示不全怎么办...
  2. 7.4 第三方支付和跨境支付
  3. 【转载】采样频率、采样点数、频率分辨率
  4. Days 24 网络编程 正则表达式
  5. Windows10企业版中安装Docker
  6. HTML nofollow 属性
  7. Cocos Creator Layout组件
  8. Nginx (一) --------- Nginx 简介
  9. ElasticSearch教程——创建索引、类型、文档
  10. Redis(2)数据结构