飞鸟系统JavaScript中的新圣箭头盘口搭建函数:如何使用Fat & Concise微投语法(上)


飞鸟系统JavaScript中的新圣箭头盘口搭建函数:如何使用Fat & Concise微投语法(上)    145

箭头函数语法:重写常规函数 函数就像食谱一样,您可以在其中存储有用的指令来完成您需要在程序中发生的事情,例如执行操作或返回值。通过调用您的函数,您可以执行配方中包含的步骤。每次调用该函数时都可以这样做,而无需一次又一次地重写配方。

这是声明函数然后在 JavaScript 中调用它的标准方法:

<span style="background-color:#292d3e"><span style="color:#bfc7d5"><code class="language-javascript"><span style="color:#697098">// function declaration</span>
function <span style="color:#82aaff">sayHiStranger</span><span style="color:#c792ea">(</span><span style="color:#c792ea">)</span> <span style="color:#c792ea">{</span>return <span style="color:#c3e88d">Hi, stranger!</span>
<span style="color:#c792ea">}</span>

<span style="color:#697098">// call the function</span>
<span style="color:#82aaff">sayHiStranger</span><span style="color:#c792ea">(</span><span style="color:#c792ea">)</span>
</code></span></span>

您还可以编写与函数表达式相同的函数,如下所示:

<span style="background-color:#292d3e"><span style="color:#bfc7d5"><code class="language-javascript">const <span style="color:#82aaff">sayHiStranger</span> <span style="color:#89ddff">=</span> function <span style="color:#c792ea">(</span><span style="color:#c792ea">)</span> <span style="color:#c792ea">{</span>return <span style="color:#c3e88d">Hi, stranger!</span>
<span style="color:#c792ea">}</span>
</code></span></span>

JavaScript 箭头函数始终是表达式。以下是使用粗箭头符号重写上述函数的方法:

<span style="background-color:#292d3e"><span style="color:#bfc7d5"><code class="language-javascript">const <span style="color:#82aaff">sayHiStranger</span> <span style="color:#89ddff">=</span> <span style="color:#c792ea">(</span><span style="color:#c792ea">)</span> <span style="color:#89ddff">=></span> <span style="color:#c3e88d">Hi, stranger</span>
</code></span></span>

这样做的好处包括:

只有一行代码 没有function关键字 没有return关键字 并且没有花括号 {} 在 JavaScript 中,函数是“一等公民”。您可以将函数存储在变量中,将它们作为参数传递给其他函数,并将它们作为值从其他函数返回。您可以使用 JavaScript 箭头函数完成所有这些操作。

无括号语法 在上面的例子中,函数没有参数。()在这种情况下,您必须在粗箭头 ( =>) 符号之前添加一组空括号。当您有多个参数时也是如此:

<span style="background-color:#292d3e"><span style="color:#bfc7d5"><code class="language-javascript">const <span style="color:#82aaff">getNetflixSeries</span> <span style="color:#89ddff">=</span> <span style="color:#c792ea">(</span>seriesName<span style="color:#c792ea">,</span> releaseDate<span style="color:#c792ea">)</span> <span style="color:#89ddff">=></span> <span style="color:#c3e88d">`</span><span style="color:#c3e88d">The </span><span style="color:#c792ea">${</span>seriesName<span style="color:#c792ea">}</span><span style="color:#c3e88d"> series was released in </span><span style="color:#c792ea">${</span>releaseDate<span style="color:#c792ea">}</span><span style="color:#c3e88d">`</span>
<span style="color:#697098">// call the function</span>
<span style="color:#ffcb6b">console</span><span style="color:#c792ea">.</span><span style="color:#82aaff">log</span><span style="color:#c792ea">(</span><span style="color:#82aaff">getNetflixSeries</span><span style="color:#c792ea">(</span><span style="color:#c3e88d">Bridgerton</span><span style="color:#c792ea">,</span> <span style="color:#c3e88d">2020</span><span style="color:#c792ea">)</span> <span style="color:#c792ea">)</span>
<span style="color:#697098">// output: The Bridgerton series was released in 2020</span>
</code></span></span>

但是,只需一个参数,您就可以继续省略括号(您不必这样做,但可以):

<span style="background-color:#292d3e"><span style="color:#bfc7d5"><code class="language-javascript">const <span style="color:#82aaff">favoriteSeries</span> <span style="color:#89ddff">=</span> seriesName <span style="color:#89ddff">=></span> seriesName <span style="color:#89ddff">===</span> <span style="color:#c3e88d">"Bridgerton"</span> <span style="color:#89ddff">?</span> <span style="color:#c3e88d">"Lets watch it"</span> <span style="color:#89ddff">:</span> <span style="color:#c3e88d">"Lets go out"</span>
<span style="color:#697098">// call the function</span>
<span style="color:#ffcb6b">console</span><span style="color:#c792ea">.</span><span style="color:#82aaff">log</span><span style="color:#c792ea">(</span><span style="color:#82aaff">favoriteSeries</span><span style="color:#c792ea">(</span><span style="color:#c3e88d">"Bridgerton"</span><span style="color:#c792ea">)</span><span style="color:#c792ea">)</span>
<span style="color:#697098">// output: "Lets watch it"</span>
</code></span></span>

不过要小心。例如,如果您决定使用默认参数,则必须将其包装在括号内:

<span style="background-color:#292d3e"><span style="color:#bfc7d5"><code class="language-javascript"><span style="color:#697098">// with parentheses: correct</span>
const bestNetflixSeries <span style="color:#89ddff">=</span> <span style="color:#c792ea">(</span>seriesName <span style="color:#89ddff">=</span> <span style="color:#c3e88d">"Bridgerton"</span><span style="color:#c792ea">)</span> <span style="color:#89ddff">=></span> <span style="color:#c3e88d">`</span><span style="color:#c792ea">${</span>seriesName<span style="color:#c792ea">}</span><span style="color:#c3e88d"> is the best</span><span style="color:#c3e88d">`</span>
<span style="color:#697098">// outputs: "Bridgerton is the best"</span>
<span style="color:#ffcb6b">console</span><span style="color:#c792ea">.</span><span style="color:#82aaff">log</span><span style="color:#c792ea">(</span><span style="color:#82aaff">bestNetflixSeries</span><span style="color:#c792ea">(</span><span style="color:#c792ea">)</span><span style="color:#c792ea">)</span>

<span style="color:#697098">// no parentheses: error</span>
const bestNetflixSeries <span style="color:#89ddff">=</span> seriesName <span style="color:#89ddff">=</span> <span style="color:#c3e88d">"Bridgerton"</span> <span style="color:#89ddff">=></span> <span style="color:#c3e88d">`</span><span style="color:#c792ea">${</span>seriesName<span style="color:#c792ea">}</span><span style="color:#c3e88d"> is the best</span><span style="color:#c3e88d">`</span>
<span style="color:#697098">// Uncaught SyntaxError: invalid arrow-function arguments (parentheses around the arrow-function may help)</span>
</code></span></span>

仅仅因为你可以,并不意味着你应该。Kyle Simpson(以You Don’t Know JS成名)夹杂着一点轻松、善意的讽刺,将他对省略括号的想法写进了这个流程图。

隐式返回 当函数体中只有一个表达式时,可以使 ES6 箭头语法更加简洁。您可以将所有内容保持在一行,删除花括号,并取消return关键字。

<span style="background-color:#292d3e"><span style="color:#bfc7d5"><code class="language-javascript"><span style="color:#697098">// using the JS sort() function to sort the titles in descending order </span>
<span style="color:#697098">// according to the number of likes (more likes at the top, fewer at the bottom</span>
const orderByLikes <span style="color:#89ddff">=</span> netflixSeries<span style="color:#c792ea">.</span><span style="color:#82aaff">sort</span><span style="color:#c792ea">(</span> <span style="color:#c792ea">(</span>a<span style="color:#c792ea">,</span> b<span style="color:#c792ea">)</span> <span style="color:#89ddff">=></span> b<span style="color:#c792ea">.</span>likes <span style="color:#89ddff">-</span> a<span style="color:#c792ea">.</span>likes <span style="color:#c792ea">)</span>

<span style="color:#697098">// call the function </span>
<span style="color:#697098">// output:the titles and the n. of likes in descending order</span>
<span style="color:#ffcb6b">console</span><span style="color:#c792ea">.</span><span style="color:#82aaff">log</span><span style="color:#c792ea">(</span>orderByLikes<span style="color:#c792ea">)</span>
</code></span></span>

这很酷,但要注意代码的可读性——尤其是在使用单行和无括号的 ES6 箭头语法对一堆箭头函数进行排序时,就像在这个例子中一样:

<span style="background-color:#292d3e"><span style="color:#bfc7d5"><code class="language-javascript">const <span style="color:#82aaff">greeter</span> <span style="color:#89ddff">=</span> greeting <span style="color:#89ddff">=></span> name <span style="color:#89ddff">=></span> <span style="color:#c3e88d">`</span><span style="color:#c792ea">${</span>greeting<span style="color:#c792ea">}</span><span style="color:#c3e88d">, </span><span style="color:#c792ea">${</span>name<span style="color:#c792ea">}</span><span style="color:#c3e88d">!</span><span style="color:#c3e88d">`</span>
</code></span></span>

那里发生了什么事?尝试使用常规函数语法:

<span style="background-color:#292d3e"><span style="color:#bfc7d5"><code class="language-javascript">function <span style="color:#82aaff">greeter</span><span style="color:#c792ea">(</span>greeting<span style="color:#c792ea">)</span> <span style="color:#c792ea">{</span>return function<span style="color:#c792ea">(</span>name<span style="color:#c792ea">)</span> <span style="color:#c792ea">{</span>return <span style="color:#c3e88d">`</span><span style="color:#c792ea">${</span>greeting<span style="color:#c792ea">}</span><span style="color:#c3e88d">, </span><span style="color:#c792ea">${</span>name<span style="color:#c792ea">}</span><span style="color:#c3e88d">!</span><span style="color:#c3e88d">`</span> <span style="color:#c792ea">}</span>
<span style="color:#c792ea">}</span> 
</code></span></span>

现在,您可以快速了解外部函数如何greeter具有参数greeting,并返回匿名函数。这个内部函数又调用了一个参数,并使用和name的值返回一个字符串。以下是调用函数的方法:greetingname

<span style="background-color:#292d3e"><span style="color:#bfc7d5"><code class="language-javascript">const myGreet <span style="color:#89ddff">=</span> <span style="color:#82aaff">greeter</span><span style="color:#c792ea">(</span><span style="color:#c3e88d">Good morning</span><span style="color:#c792ea">)</span>
<span style="color:#ffcb6b">console</span><span style="color:#c792ea">.</span><span style="color:#82aaff">log</span><span style="color:#c792ea">(</span> <span style="color:#82aaff">myGreet</span><span style="color:#c792ea">(</span><span style="color:#c3e88d">Mary</span><span style="color:#c792ea">)</span> <span style="color:#c792ea">)</span> 

<span style="color:#697098">// output: </span>
<span style="color:#c3e88d">"Good morning, Mary!"</span> 
</code></span></span>

注意这些隐式返回错误 当您的 JavaScript 箭头函数包含多个语句时,您需要将所有语句括在花括号中并使用return关键字。

在下面的代码中,该函数构建了一个对象,其中包含一些 Netflix 系列的标题和摘要(Netflix 评论来自Rotten Tomatoes网站):

<span style="background-color:#292d3e"><span style="color:#bfc7d5"><code class="language-javascript">const seriesList <span style="color:#89ddff">=</span> netflixSeries<span style="color:#c792ea">.</span><span style="color:#82aaff">map</span><span style="color:#c792ea">(</span> series <span style="color:#89ddff">=></span> <span style="color:#c792ea">{</span>const container <span style="color:#89ddff">=</span> <span style="color:#c792ea">{</span><span style="color:#c792ea">}</span>container<span style="color:#c792ea">.</span>title <span style="color:#89ddff">=</span> series<span style="color:#c792ea">.</span>name container<span style="color:#c792ea">.</span>summary <span style="color:#89ddff">=</span> series<span style="color:#c792ea">.</span>summary<span style="color:#697098">// explicit return</span>return container
<span style="color:#c792ea">}</span> <span style="color:#c792ea">)</span>
</code></span></span>

函数内部的箭头函数.map()由一系列语句发展而来,在语句结束时它返回一个对象。这使得在函数体周围使用花括号是不可避免的。

此外,当您使用花括号时,隐式返回不是一种选择。您必须使用return关键字。

如果您的函数使用隐式返回返回对象字面量,则需要将对象包装在圆括号内。不这样做会导致错误,因为 JavaScript 引擎错误地将对象字面量的花括号解析为函数的花括号。正如您在上面刚刚注意到的,当您在箭头函数中使用花括号时,您不能省略 return 关键字。

前面代码的较短版本演示了这种语法:

<span style="background-color:#292d3e"><span style="color:#bfc7d5"><code class="language-javascript"><span style="color:#697098">// Uncaught SyntaxError: unexpected token: :</span>
const seriesList <span style="color:#89ddff">=</span> netflixSeries<span style="color:#c792ea">.</span><span style="color:#82aaff">map</span><span style="color:#c792ea">(</span>series <span style="color:#89ddff">=></span> <span style="color:#c792ea">{</span> title<span style="color:#89ddff">:</span> series<span style="color:#c792ea">.</span>name <span style="color:#c792ea">}</span><span style="color:#c792ea">)</span><span style="color:#c792ea">;</span>

<span style="color:#697098">// Works fine</span>
const seriesList <span style="color:#89ddff">=</span> netflixSeries<span style="color:#c792ea">.</span><span style="color:#82aaff">map</span><span style="color:#c792ea">(</span>series <span style="color:#89ddff">=></span> <span style="color:#c792ea">(</span><span style="color:#c792ea">{</span> title<span style="color:#89ddff">:</span> series<span style="color:#c792ea">.</span>name <span style="color:#c792ea">}</span><span style="color:#c792ea">)</span><span style="color:#c792ea">)</span><span style="color:#c792ea">;</span>
</code></span></span>

你不能命名箭头函数 function在关键字和参数列表之间没有名称标识符的函数称为匿名函数。以下是常规匿名函数表达式的样子:

<span style="background-color:#292d3e"><span style="color:#bfc7d5"><code class="language-javascript">const <span style="color:#82aaff">anonymous</span> <span style="color:#89ddff">=</span> function<span style="color:#c792ea">(</span><span style="color:#c792ea">)</span> <span style="color:#c792ea">{</span>return <span style="color:#c3e88d">You can	 identify me!</span> 
<span style="color:#c792ea">}</span>
</code></span></span>

箭头函数都是匿名函数:

<span style="background-color:#292d3e"><span style="color:#bfc7d5"><code class="language-javascript">const <span style="color:#82aaff">anonymousArrowFunc</span> <span style="color:#89ddff">=</span> <span style="color:#c792ea">(</span><span style="color:#c792ea">)</span> <span style="color:#89ddff">=></span> <span style="color:#c3e88d">You can	 identify me!</span> 
</code></span></span>

name从 ES6 开始,变量和方法可以使用匿名函数的属性从其语法位置推断出匿名函数的名称。这使得在检查其值或报告错误时可以识别该函数。

使用以下方法检查anonymousArrowFunc:

<span style="background-color:#292d3e"><span style="color:#bfc7d5"><code class="language-javascript"><span style="color:#ffcb6b">console</span><span style="color:#c792ea">.</span><span style="color:#82aaff">log</span><span style="color:#c792ea">(</span>anonymousArrowFunc<span style="color:#c792ea">.</span>name<span style="color:#c792ea">)</span>
<span style="color:#697098">// output: "anonymousArrowFunc"</span>
</code></span></span>

请注意,此推断name属性仅在将匿名函数分配给变量时才存在,如上面的示例中所示。如果您使用匿名函数作为回调,您将失去这个有用的功能。这在下面的演示中得到了例证,其中方法内的匿名函数.setInterval()无法利用该name属性:

<span style="background-color:#292d3e"><span style="color:#bfc7d5"><code class="language-javascript">let counter <span style="color:#89ddff">=</span> <span style="color:#f78c6c">5</span>
let countDown <span style="color:#89ddff">=</span> <span style="color:#82aaff">setInterval</span><span style="color:#c792ea">(</span><span style="color:#c792ea">(</span><span style="color:#c792ea">)</span> <span style="color:#89ddff">=></span> <span style="color:#c792ea">{</span><span style="color:#ffcb6b">console</span><span style="color:#c792ea">.</span><span style="color:#82aaff">log</span><span style="color:#c792ea">(</span>counter<span style="color:#c792ea">)</span>counter<span style="color:#89ddff">--</span>if <span style="color:#c792ea">(</span>counter <span style="color:#89ddff">===</span> <span style="color:#f78c6c">0</span><span style="color:#c792ea">)</span> <span style="color:#c792ea">{</span><span style="color:#ffcb6b">console</span><span style="color:#c792ea">.</span><span style="color:#82aaff">log</span><span style="color:#c792ea">(</span><span style="color:#c3e88d">"I have no name!!"</span><span style="color:#c792ea">)</span><span style="color:#82aaff">clearInterval</span><span style="color:#c792ea">(</span>countDown<span style="color:#c792ea">)</span><span style="color:#c792ea">}</span>
<span style="color:#c792ea">}</span><span style="color:#c792ea">,</span> <span style="color:#f78c6c">1000</span><span style="color:#c792ea">)</span>
</code></span></span>

这还不是全部。这个推断name的属性仍然不能作为一个正确的标识符,你可以使用它来从内部引用函数——例如递归、解除绑定事件等。

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/290250.html

(0)
上一篇 2022年10月1日
下一篇 2022年10月1日

发表回复

登录后才能评论