113.9322f157.js 50 KB

1
  1. (window.webpackJsonp=window.webpackJsonp||[]).push([[113],{443:function(t,e,a){"use strict";a.r(e);var s=a(3),n=Object(s.a)({},(function(){var t=this,e=t._self._c;return e("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[e("h1",{attrs:{id:"读懂-ecmascript-规格"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#读懂-ecmascript-规格"}},[t._v("#")]),t._v(" 读懂 ECMAScript 规格")]),t._v(" "),e("h2",{attrs:{id:"概述"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#概述"}},[t._v("#")]),t._v(" 概述")]),t._v(" "),e("p",[t._v("规格文件是计算机语言的官方标准,详细描述语法规则和实现方法。")]),t._v(" "),e("p",[t._v("一般来说,没有必要阅读规格,除非你要写编译器。因为规格写得非常抽象和精炼,又缺乏实例,不容易理解,而且对于解决实际的应用问题,帮助不大。但是,如果你遇到疑难的语法问题,实在找不到答案,这时可以去查看规格文件,了解语言标准是怎么说的。规格是解决问题的“最后一招”。\n"),t._v("\n这对 JavaScript 语言很有必要。因为它的使用场景复杂,语法规则不统一,例外很多,各种运行环境的行为不一致,导致奇怪的语法问题层出不穷,任何语法书都不可能囊括所有情况。查看规格,不失为一种解决语法问题的最可靠、最权威的终极方法。")]),t._v(" "),e("p",[t._v("本章介绍如何读懂 ECMAScript 6 的规格文件。")]),t._v(" "),e("p",[t._v("ECMAScript 6 的规格,可以在 ECMA 国际标准组织的官方网站("),e("a",{attrs:{href:"http://www.ecma-international.org/ecma-262/6.0/",target:"_blank",rel:"noopener noreferrer"}},[t._v("www.ecma-international.org/ecma-262/6.0/"),e("OutboundLink")],1),t._v(")免费下载和在线阅读。")]),t._v(" "),e("p",[t._v("这个规格文件相当庞大,一共有 26 章,A4 打印的话,足足有 545 页。它的特点就是规定得非常细致,每一个语法行为、每一个函数的实现都做了详尽的清晰的描述。基本上,编译器作者只要把每一步翻译成代码就可以了。这很大程度上,保证了所有 ES6 实现都有一致的行为。")]),t._v(" "),e("p",[t._v("ECMAScript 6 规格的 26 章之中,第 1 章到第 3 章是对文件本身的介绍,与语言关系不大。第 4 章是对这门语言总体设计的描述,有兴趣的读者可以读一下。第 5 章到第 8 章是语言宏观层面的描述。第 5 章是规格的名词解释和写法的介绍,第 6 章介绍数据类型,第 7 章介绍语言内部用到的抽象操作,第 8 章介绍代码如何运行。第 9 章到第 26 章介绍具体的语法。")]),t._v(" "),e("p",[t._v("对于一般用户来说,除了第 4 章,其他章节都涉及某一方面的细节,不用通读,只要在用到的时候,查阅相关章节即可。")]),t._v(" "),e("h2",{attrs:{id:"术语"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#术语"}},[t._v("#")]),t._v(" 术语")]),t._v(" "),e("p",[t._v("ES6 规格使用了一些专门的术语,了解这些术语,可以帮助你读懂规格。本节介绍其中的几个。")]),t._v(" "),e("h3",{attrs:{id:"抽象操作"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#抽象操作"}},[t._v("#")]),t._v(" 抽象操作")]),t._v(" "),e("p",[t._v("所谓“抽象操作”(abstract operations)就是引擎的一些内部方法,外部不能调用。规格定义了一系列的抽象操作,规定了它们的行为,留给各种引擎自己去实现。")]),t._v(" "),e("p",[t._v("举例来说,"),e("code",[t._v("Boolean(value)")]),t._v("的算法,第一步是这样的。")]),t._v(" "),e("blockquote",[e("ol",[e("li",[t._v("Let "),e("code",[t._v("b")]),t._v(" be "),e("code",[t._v("ToBoolean(value)")]),t._v(".")])])]),t._v(" "),e("p",[t._v("这里的"),e("code",[t._v("ToBoolean")]),t._v("就是一个抽象操作,是引擎内部求出布尔值的算法。")]),t._v(" "),e("p",[t._v("许多函数的算法都会多次用到同样的步骤,所以 ES6 规格将它们抽出来,定义成“抽象操作”,方便描述。")]),t._v(" "),e("h3",{attrs:{id:"record-和-field"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#record-和-field"}},[t._v("#")]),t._v(" Record 和 field")]),t._v(" "),e("p",[t._v("ES6 规格将键值对(key-value map)的数据结构称为 Record,其中的每一组键值对称为 field。这就是说,一个 Record 由多个 field 组成,而每个 field 都包含一个键名(key)和一个键值(value)。")]),t._v(" "),e("h3",{attrs:{id:"notation"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#notation"}},[t._v("#")]),t._v(" [[Notation]]")]),t._v(" "),e("p",[t._v("ES6 规格大量使用"),e("code",[t._v("[[Notation]]")]),t._v("这种书写法,比如"),e("code",[t._v("[[Value]]")]),t._v("、"),e("code",[t._v("[[Writable]]")]),t._v("、"),e("code",[t._v("[[Get]]")]),t._v("、"),e("code",[t._v("[[Set]]")]),t._v("等等。它用来指代 field 的键名。")]),t._v(" "),e("p",[t._v("举例来说,"),e("code",[t._v("obj")]),t._v("是一个 Record,它有一个"),e("code",[t._v("Prototype")]),t._v("属性。ES6 规格不会写"),e("code",[t._v("obj.Prototype")]),t._v(",而是写"),e("code",[t._v("obj.[[Prototype]]")]),t._v("。一般来说,使用"),e("code",[t._v("[[Notation]]")]),t._v("这种书写法的属性,都是对象的内部属性。")]),t._v(" "),e("p",[t._v("所有的 JavaScript 函数都有一个内部属性"),e("code",[t._v("[[Call]]")]),t._v(",用来运行该函数。")]),t._v(" "),e("div",{staticClass:"language-javascript line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-javascript"}},[e("code",[e("span",{pre:!0,attrs:{class:"token constant"}},[t._v("F")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("Call"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token constant"}},[t._v("V")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" argumentsList"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br")])]),e("p",[t._v("上面代码中,"),e("code",[t._v("F")]),t._v("是一个函数对象,"),e("code",[t._v("[[Call]]")]),t._v("是它的内部方法,"),e("code",[t._v("F.[[call]]()")]),t._v("表示运行该函数,"),e("code",[t._v("V")]),t._v("表示"),e("code",[t._v("[[Call]]")]),t._v("运行时"),e("code",[t._v("this")]),t._v("的值,"),e("code",[t._v("argumentsList")]),t._v("则是调用时传入函数的参数。")]),t._v(" "),e("h3",{attrs:{id:"completion-record"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#completion-record"}},[t._v("#")]),t._v(" Completion Record")]),t._v(" "),e("p",[t._v("每一个语句都会返回一个 Completion Record,表示运行结果。每个 Completion Record 有一个"),e("code",[t._v("[[Type]]")]),t._v("属性,表示运行结果的类型。")]),t._v(" "),e("p",[e("code",[t._v("[[Type]]")]),t._v("属性有五种可能的值。")]),t._v(" "),e("ul",[e("li",[t._v("normal")]),t._v(" "),e("li",[t._v("return")]),t._v(" "),e("li",[t._v("throw")]),t._v(" "),e("li",[t._v("break")]),t._v(" "),e("li",[t._v("continue")])]),t._v(" "),e("p",[t._v("如果"),e("code",[t._v("[[Type]]")]),t._v("的值是"),e("code",[t._v("normal")]),t._v(",就称为 normal completion,表示运行正常。其他的值,都称为 abrupt completion。其中,开发者只需要关注"),e("code",[t._v("[[Type]]")]),t._v("为"),e("code",[t._v("throw")]),t._v("的情况,即运行出错;"),e("code",[t._v("break")]),t._v("、"),e("code",[t._v("continue")]),t._v("、"),e("code",[t._v("return")]),t._v("这三个值都只出现在特定场景,可以不用考虑。")]),t._v(" "),e("h2",{attrs:{id:"抽象操作的标准流程"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#抽象操作的标准流程"}},[t._v("#")]),t._v(" 抽象操作的标准流程")]),t._v(" "),e("p",[t._v("抽象操作的运行流程,一般是下面这样。")]),t._v(" "),e("blockquote",[e("ol",[e("li",[t._v("Let "),e("code",[t._v("resultCompletionRecord")]),t._v(" be "),e("code",[t._v("AbstractOp()")]),t._v(".")]),t._v(" "),e("li",[t._v("If "),e("code",[t._v("resultCompletionRecord")]),t._v(" is an abrupt completion, return "),e("code",[t._v("resultCompletionRecord")]),t._v(".")]),t._v(" "),e("li",[t._v("Let "),e("code",[t._v("result")]),t._v(" be "),e("code",[t._v("resultCompletionRecord.[[Value]]")]),t._v(".")]),t._v(" "),e("li",[t._v("return "),e("code",[t._v("result")]),t._v(".")])])]),t._v(" "),e("p",[t._v("上面的第一步是调用抽象操作"),e("code",[t._v("AbstractOp()")]),t._v(",得到"),e("code",[t._v("resultCompletionRecord")]),t._v(",这是一个 Completion Record。第二步,如果这个 Record 属于 abrupt completion,就将"),e("code",[t._v("resultCompletionRecord")]),t._v("返回给用户。如果此处没有返回,就表示运行结果正常,所得的值存放在"),e("code",[t._v("resultCompletionRecord.[[Value]]")]),t._v("属性。第三步,将这个值记为"),e("code",[t._v("result")]),t._v("。第四步,将"),e("code",[t._v("result")]),t._v("返回给用户。")]),t._v(" "),e("p",[t._v("ES6 规格将这个标准流程,使用简写的方式表达。")]),t._v(" "),e("blockquote",[e("ol",[e("li",[t._v("Let "),e("code",[t._v("result")]),t._v(" be "),e("code",[t._v("AbstractOp()")]),t._v(".")]),t._v(" "),e("li",[e("code",[t._v("ReturnIfAbrupt(result)")]),t._v(".")]),t._v(" "),e("li",[t._v("return "),e("code",[t._v("result")]),t._v(".")])])]),t._v(" "),e("p",[t._v("这个简写方式里面的"),e("code",[t._v("ReturnIfAbrupt(result)")]),t._v(",就代表了上面的第二步和第三步,即如果有报错,就返回错误,否则取出值。")]),t._v(" "),e("p",[t._v("甚至还有进一步的简写格式。")]),t._v(" "),e("blockquote",[e("ol",[e("li",[t._v("Let "),e("code",[t._v("result")]),t._v(" be "),e("code",[t._v("? AbstractOp()")]),t._v(".")]),t._v(" "),e("li",[t._v("return "),e("code",[t._v("result")]),t._v(".")])])]),t._v(" "),e("p",[t._v("上面流程的"),e("code",[t._v("?")]),t._v(",就代表"),e("code",[t._v("AbstractOp()")]),t._v("可能会报错。一旦报错,就返回错误,否则取出值。")]),t._v(" "),e("p",[t._v("除了"),e("code",[t._v("?")]),t._v(",ES 6 规格还使用另一个简写符号"),e("code",[t._v("!")]),t._v("。")]),t._v(" "),e("blockquote",[e("ol",[e("li",[t._v("Let "),e("code",[t._v("result")]),t._v(" be "),e("code",[t._v("! AbstractOp()")]),t._v(".")]),t._v(" "),e("li",[t._v("return "),e("code",[t._v("result")]),t._v(".")])])]),t._v(" "),e("p",[t._v("上面流程的"),e("code",[t._v("!")]),t._v(",代表"),e("code",[t._v("AbstractOp()")]),t._v("不会报错,返回的一定是 normal completion,总是可以取出值。")]),t._v(" "),e("h2",{attrs:{id:"相等运算符"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#相等运算符"}},[t._v("#")]),t._v(" 相等运算符")]),t._v(" "),e("p",[t._v("下面通过一些例子,介绍如何使用这份规格。")]),t._v(" "),e("p",[t._v("相等运算符("),e("code",[t._v("==")]),t._v(")是一个很让人头痛的运算符,它的语法行为多变,不符合直觉。这个小节就看看规格怎么规定它的行为。")]),t._v(" "),e("p",[t._v("请看下面这个表达式,请问它的值是多少。")]),t._v(" "),e("div",{staticClass:"language-javascript line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-javascript"}},[e("code",[e("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("==")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("null")]),t._v("\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br")])]),e("p",[t._v("如果你不确定答案,或者想知道语言内部怎么处理,就可以去查看规格,"),e("a",{attrs:{href:"http://www.ecma-international.org/ecma-262/6.0/#sec-abstract-equality-comparison",target:"_blank",rel:"noopener noreferrer"}},[t._v("7.2.12 小节"),e("OutboundLink")],1),t._v("是对相等运算符("),e("code",[t._v("==")]),t._v(")的描述。")]),t._v(" "),e("p",[t._v("规格对每一种语法行为的描述,都分成两部分:先是总体的行为描述,然后是实现的算法细节。相等运算符的总体描述,只有一句话。")]),t._v(" "),e("blockquote",[e("p",[t._v("“The comparison "),e("code",[t._v("x == y")]),t._v(", where "),e("code",[t._v("x")]),t._v(" and "),e("code",[t._v("y")]),t._v(" are values, produces "),e("code",[t._v("true")]),t._v(" or "),e("code",[t._v("false")]),t._v(".”")])]),t._v(" "),e("p",[t._v("上面这句话的意思是,相等运算符用于比较两个值,返回"),e("code",[t._v("true")]),t._v("或"),e("code",[t._v("false")]),t._v("。")]),t._v(" "),e("p",[t._v("下面是算法细节。")]),t._v(" "),e("blockquote",[e("ol",[e("li",[t._v("ReturnIfAbrupt(x).")]),t._v(" "),e("li",[t._v("ReturnIfAbrupt(y).")]),t._v(" "),e("li",[t._v("If "),e("code",[t._v("Type(x)")]),t._v(" is the same as "),e("code",[t._v("Type(y)")]),t._v(", then\n"),e("ol",[e("li",[t._v("Return the result of performing Strict Equality Comparison "),e("code",[t._v("x === y")]),t._v(".")])])]),t._v(" "),e("li",[t._v("If "),e("code",[t._v("x")]),t._v(" is "),e("code",[t._v("null")]),t._v(" and "),e("code",[t._v("y")]),t._v(" is "),e("code",[t._v("undefined")]),t._v(", return "),e("code",[t._v("true")]),t._v(".")]),t._v(" "),e("li",[t._v("If "),e("code",[t._v("x")]),t._v(" is "),e("code",[t._v("undefined")]),t._v(" and "),e("code",[t._v("y")]),t._v(" is "),e("code",[t._v("null")]),t._v(", return "),e("code",[t._v("true")]),t._v(".")]),t._v(" "),e("li",[t._v("If "),e("code",[t._v("Type(x)")]),t._v(" is Number and "),e("code",[t._v("Type(y)")]),t._v(" is String,\nreturn the result of the comparison "),e("code",[t._v("x == ToNumber(y)")]),t._v(".")]),t._v(" "),e("li",[t._v("If "),e("code",[t._v("Type(x)")]),t._v(" is String and "),e("code",[t._v("Type(y)")]),t._v(" is Number,\nreturn the result of the comparison "),e("code",[t._v("ToNumber(x) == y")]),t._v(".")]),t._v(" "),e("li",[t._v("If "),e("code",[t._v("Type(x)")]),t._v(" is Boolean, return the result of the comparison "),e("code",[t._v("ToNumber(x) == y")]),t._v(".")]),t._v(" "),e("li",[t._v("If "),e("code",[t._v("Type(y)")]),t._v(" is Boolean, return the result of the comparison "),e("code",[t._v("x == ToNumber(y)")]),t._v(".")]),t._v(" "),e("li",[t._v("If "),e("code",[t._v("Type(x)")]),t._v(" is either String, Number, or Symbol and "),e("code",[t._v("Type(y)")]),t._v(" is Object, then\nreturn the result of the comparison "),e("code",[t._v("x == ToPrimitive(y)")]),t._v(".")]),t._v(" "),e("li",[t._v("If "),e("code",[t._v("Type(x)")]),t._v(" is Object and "),e("code",[t._v("Type(y)")]),t._v(" is either String, Number, or Symbol, then\nreturn the result of the comparison "),e("code",[t._v("ToPrimitive(x) == y")]),t._v(".")]),t._v(" "),e("li",[t._v("Return "),e("code",[t._v("false")]),t._v(".")])])]),t._v(" "),e("p",[t._v("上面这段算法,一共有 12 步,翻译如下。")]),t._v(" "),e("blockquote",[e("ol",[e("li",[t._v("如果"),e("code",[t._v("x")]),t._v("不是正常值(比如抛出一个错误),中断执行。")]),t._v(" "),e("li",[t._v("如果"),e("code",[t._v("y")]),t._v("不是正常值,中断执行。")]),t._v(" "),e("li",[t._v("如果"),e("code",[t._v("Type(x)")]),t._v("与"),e("code",[t._v("Type(y)")]),t._v("相同,执行严格相等运算"),e("code",[t._v("x === y")]),t._v("。")]),t._v(" "),e("li",[t._v("如果"),e("code",[t._v("x")]),t._v("是"),e("code",[t._v("null")]),t._v(","),e("code",[t._v("y")]),t._v("是"),e("code",[t._v("undefined")]),t._v(",返回"),e("code",[t._v("true")]),t._v("。")]),t._v(" "),e("li",[t._v("如果"),e("code",[t._v("x")]),t._v("是"),e("code",[t._v("undefined")]),t._v(","),e("code",[t._v("y")]),t._v("是"),e("code",[t._v("null")]),t._v(",返回"),e("code",[t._v("true")]),t._v("。")]),t._v(" "),e("li",[t._v("如果"),e("code",[t._v("Type(x)")]),t._v("是数值,"),e("code",[t._v("Type(y)")]),t._v("是字符串,返回"),e("code",[t._v("x == ToNumber(y)")]),t._v("的结果。")]),t._v(" "),e("li",[t._v("如果"),e("code",[t._v("Type(x)")]),t._v("是字符串,"),e("code",[t._v("Type(y)")]),t._v("是数值,返回"),e("code",[t._v("ToNumber(x) == y")]),t._v("的结果。")]),t._v(" "),e("li",[t._v("如果"),e("code",[t._v("Type(x)")]),t._v("是布尔值,返回"),e("code",[t._v("ToNumber(x) == y")]),t._v("的结果。")]),t._v(" "),e("li",[t._v("如果"),e("code",[t._v("Type(y)")]),t._v("是布尔值,返回"),e("code",[t._v("x == ToNumber(y)")]),t._v("的结果。")]),t._v(" "),e("li",[t._v("如果"),e("code",[t._v("Type(x)")]),t._v("是字符串或数值或"),e("code",[t._v("Symbol")]),t._v("值,"),e("code",[t._v("Type(y)")]),t._v("是对象,返回"),e("code",[t._v("x == ToPrimitive(y)")]),t._v("的结果。")]),t._v(" "),e("li",[t._v("如果"),e("code",[t._v("Type(x)")]),t._v("是对象,"),e("code",[t._v("Type(y)")]),t._v("是字符串或数值或"),e("code",[t._v("Symbol")]),t._v("值,返回"),e("code",[t._v("ToPrimitive(x) == y")]),t._v("的结果。")]),t._v(" "),e("li",[t._v("返回"),e("code",[t._v("false")]),t._v("。")])])]),t._v(" "),e("p",[t._v("由于"),e("code",[t._v("0")]),t._v("的类型是数值,"),e("code",[t._v("null")]),t._v("的类型是 Null(这是规格"),e("a",{attrs:{href:"http://www.ecma-international.org/ecma-262/6.0/#sec-terms-and-definitions-null-type",target:"_blank",rel:"noopener noreferrer"}},[t._v("4.3.13 小节"),e("OutboundLink")],1),t._v("的规定,是内部 Type 运算的结果,跟"),e("code",[t._v("typeof")]),t._v("运算符无关)。因此上面的前 11 步都得不到结果,要到第 12 步才能得到"),e("code",[t._v("false")]),t._v("。")]),t._v(" "),e("div",{staticClass:"language-javascript line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-javascript"}},[e("code",[e("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("==")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("null")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// false")]),t._v("\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br")])]),e("h2",{attrs:{id:"数组的空位"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#数组的空位"}},[t._v("#")]),t._v(" 数组的空位")]),t._v(" "),e("p",[t._v("下面再看另一个例子。")]),t._v(" "),e("div",{staticClass:"language-javascript line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-javascript"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("const")]),t._v(" a1 "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("undefined")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("undefined")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("undefined")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("const")]),t._v(" a2 "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n\na1"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("length "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 3")]),t._v("\na2"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("length "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 3")]),t._v("\n\na1"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// undefined")]),t._v("\na2"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// undefined")]),t._v("\n\na1"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("===")]),t._v(" a2"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// true")]),t._v("\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br"),e("span",{staticClass:"line-number"},[t._v("3")]),e("br"),e("span",{staticClass:"line-number"},[t._v("4")]),e("br"),e("span",{staticClass:"line-number"},[t._v("5")]),e("br"),e("span",{staticClass:"line-number"},[t._v("6")]),e("br"),e("span",{staticClass:"line-number"},[t._v("7")]),e("br"),e("span",{staticClass:"line-number"},[t._v("8")]),e("br"),e("span",{staticClass:"line-number"},[t._v("9")]),e("br"),e("span",{staticClass:"line-number"},[t._v("10")]),e("br")])]),e("p",[t._v("上面代码中,数组"),e("code",[t._v("a1")]),t._v("的成员是三个"),e("code",[t._v("undefined")]),t._v(",数组"),e("code",[t._v("a2")]),t._v("的成员是三个空位。这两个数组很相似,长度都是 3,每个位置的成员读取出来都是"),e("code",[t._v("undefined")]),t._v("。")]),t._v(" "),e("p",[t._v("但是,它们实际上存在重大差异。")]),t._v(" "),e("div",{staticClass:"language-javascript line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-javascript"}},[e("code",[e("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("in")]),t._v(" a1 "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// true")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("in")]),t._v(" a2 "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// false")]),t._v("\n\na1"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("hasOwnProperty")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// true")]),t._v("\na2"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("hasOwnProperty")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// false")]),t._v("\n\nObject"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("keys")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("a1"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v('// ["0", "1", "2"]')]),t._v("\nObject"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("keys")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("a2"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// []")]),t._v("\n\na1"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("map")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token parameter"}},[t._v("n")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=>")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("1")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// [1, 1, 1]")]),t._v("\na2"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("map")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token parameter"}},[t._v("n")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=>")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("1")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// [, , ,]")]),t._v("\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br"),e("span",{staticClass:"line-number"},[t._v("3")]),e("br"),e("span",{staticClass:"line-number"},[t._v("4")]),e("br"),e("span",{staticClass:"line-number"},[t._v("5")]),e("br"),e("span",{staticClass:"line-number"},[t._v("6")]),e("br"),e("span",{staticClass:"line-number"},[t._v("7")]),e("br"),e("span",{staticClass:"line-number"},[t._v("8")]),e("br"),e("span",{staticClass:"line-number"},[t._v("9")]),e("br"),e("span",{staticClass:"line-number"},[t._v("10")]),e("br"),e("span",{staticClass:"line-number"},[t._v("11")]),e("br")])]),e("p",[t._v("上面代码一共列出了四种运算,数组"),e("code",[t._v("a1")]),t._v("和"),e("code",[t._v("a2")]),t._v("的结果都不一样。前三种运算("),e("code",[t._v("in")]),t._v("运算符、数组的"),e("code",[t._v("hasOwnProperty")]),t._v("方法、"),e("code",[t._v("Object.keys")]),t._v("方法)都说明,数组"),e("code",[t._v("a2")]),t._v("取不到属性名。最后一种运算(数组的"),e("code",[t._v("map")]),t._v("方法)说明,数组"),e("code",[t._v("a2")]),t._v("没有发生遍历。")]),t._v(" "),e("p",[t._v("为什么"),e("code",[t._v("a1")]),t._v("与"),e("code",[t._v("a2")]),t._v("成员的行为不一致?数组的成员是"),e("code",[t._v("undefined")]),t._v("或空位,到底有什么不同?")]),t._v(" "),e("p",[t._v("规格的"),e("a",{attrs:{href:"http://www.ecma-international.org/ecma-262/6.0/#sec-array-initializer",target:"_blank",rel:"noopener noreferrer"}},[t._v("12.2.5 小节《数组的初始化》"),e("OutboundLink")],1),t._v("给出了答案。")]),t._v(" "),e("blockquote",[e("p",[t._v("“Array elements may be elided at the beginning, middle or end of the element list. Whenever a comma in the element list is not preceded by an AssignmentExpression (i.e., a comma at the beginning or after another comma), the missing array element contributes to the length of the Array and increases the index of subsequent elements. Elided array elements are not defined. If an element is elided at the end of an array, that element does not contribute to the length of the Array.”")])]),t._v(" "),e("p",[t._v("翻译如下。")]),t._v(" "),e("blockquote",[e("p",[t._v('"数组成员可以省略。只要逗号前面没有任何表达式,数组的'),e("code",[t._v("length")]),t._v("属性就会加 1,并且相应增加其后成员的位置索引。被省略的成员不会被定义。如果被省略的成员是数组最后一个成员,则不会导致数组"),e("code",[t._v("length")]),t._v("属性增加。”")])]),t._v(" "),e("p",[t._v("上面的规格说得很清楚,数组的空位会反映在"),e("code",[t._v("length")]),t._v("属性,也就是说空位有自己的位置,但是这个位置的值是未定义,即这个值是不存在的。如果一定要读取,结果就是"),e("code",[t._v("undefined")]),t._v("(因为"),e("code",[t._v("undefined")]),t._v("在 JavaScript 语言中表示不存在)。")]),t._v(" "),e("p",[t._v("这就解释了为什么"),e("code",[t._v("in")]),t._v("运算符、数组的"),e("code",[t._v("hasOwnProperty")]),t._v("方法、"),e("code",[t._v("Object.keys")]),t._v("方法,都取不到空位的属性名。因为这个属性名根本就不存在,规格里面没说要为空位分配属性名(位置索引),只说要为下一个元素的位置索引加 1。")]),t._v(" "),e("p",[t._v("至于为什么数组的"),e("code",[t._v("map")]),t._v("方法会跳过空位,请看下一节。")]),t._v(" "),e("h2",{attrs:{id:"数组的-map-方法"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#数组的-map-方法"}},[t._v("#")]),t._v(" 数组的 map 方法")]),t._v(" "),e("p",[t._v("规格的"),e("a",{attrs:{href:"http://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.map",target:"_blank",rel:"noopener noreferrer"}},[t._v("22.1.3.15 小节"),e("OutboundLink")],1),t._v("定义了数组的"),e("code",[t._v("map")]),t._v("方法。该小节先是总体描述"),e("code",[t._v("map")]),t._v("方法的行为,里面没有提到数组空位。")]),t._v(" "),e("p",[t._v("后面的算法描述是这样的。")]),t._v(" "),e("blockquote",[e("ol",[e("li",[t._v("Let "),e("code",[t._v("O")]),t._v(" be "),e("code",[t._v("ToObject(this value)")]),t._v(".")]),t._v(" "),e("li",[e("code",[t._v("ReturnIfAbrupt(O)")]),t._v(".")]),t._v(" "),e("li",[t._v("Let "),e("code",[t._v("len")]),t._v(" be "),e("code",[t._v('ToLength(Get(O, "length"))')]),t._v(".")]),t._v(" "),e("li",[e("code",[t._v("ReturnIfAbrupt(len)")]),t._v(".")]),t._v(" "),e("li",[t._v("If "),e("code",[t._v("IsCallable(callbackfn)")]),t._v(" is "),e("code",[t._v("false")]),t._v(", throw a TypeError exception.")]),t._v(" "),e("li",[t._v("If "),e("code",[t._v("thisArg")]),t._v(" was supplied, let "),e("code",[t._v("T")]),t._v(" be "),e("code",[t._v("thisArg")]),t._v("; else let "),e("code",[t._v("T")]),t._v(" be "),e("code",[t._v("undefined")]),t._v(".")]),t._v(" "),e("li",[t._v("Let "),e("code",[t._v("A")]),t._v(" be "),e("code",[t._v("ArraySpeciesCreate(O, len)")]),t._v(".")]),t._v(" "),e("li",[e("code",[t._v("ReturnIfAbrupt(A)")]),t._v(".")]),t._v(" "),e("li",[t._v("Let "),e("code",[t._v("k")]),t._v(" be 0.")]),t._v(" "),e("li",[t._v("Repeat, while "),e("code",[t._v("k")]),t._v(" < "),e("code",[t._v("len")]),t._v(" "),e("ol",[e("li",[t._v("Let "),e("code",[t._v("Pk")]),t._v(" be "),e("code",[t._v("ToString(k)")]),t._v(".")]),t._v(" "),e("li",[t._v("Let "),e("code",[t._v("kPresent")]),t._v(" be "),e("code",[t._v("HasProperty(O, Pk)")]),t._v(".")]),t._v(" "),e("li",[e("code",[t._v("ReturnIfAbrupt(kPresent)")]),t._v(".")]),t._v(" "),e("li",[t._v("If "),e("code",[t._v("kPresent")]),t._v(" is "),e("code",[t._v("true")]),t._v(", then\n"),e("ol",[e("li",[t._v("Let "),e("code",[t._v("kValue")]),t._v(" be "),e("code",[t._v("Get(O, Pk)")]),t._v(".")]),t._v(" "),e("li",[e("code",[t._v("ReturnIfAbrupt(kValue)")]),t._v(".")]),t._v(" "),e("li",[t._v("Let "),e("code",[t._v("mappedValue")]),t._v(" be "),e("code",[t._v("Call(callbackfn, T, «kValue, k, O»)")]),t._v(".")]),t._v(" "),e("li",[e("code",[t._v("ReturnIfAbrupt(mappedValue)")]),t._v(".")]),t._v(" "),e("li",[t._v("Let "),e("code",[t._v("status")]),t._v(" be "),e("code",[t._v("CreateDataPropertyOrThrow (A, Pk, mappedValue)")]),t._v(".")]),t._v(" "),e("li",[e("code",[t._v("ReturnIfAbrupt(status)")]),t._v(".")])])]),t._v(" "),e("li",[t._v("Increase "),e("code",[t._v("k")]),t._v(" by 1.")])])]),t._v(" "),e("li",[t._v("Return "),e("code",[t._v("A")]),t._v(".")])])]),t._v(" "),e("p",[t._v("翻译如下。")]),t._v(" "),e("blockquote",[e("ol",[e("li",[t._v("得到当前数组的"),e("code",[t._v("this")]),t._v("对象")]),t._v(" "),e("li",[t._v("如果报错就返回")]),t._v(" "),e("li",[t._v("求出当前数组的"),e("code",[t._v("length")]),t._v("属性")]),t._v(" "),e("li",[t._v("如果报错就返回")]),t._v(" "),e("li",[t._v("如果 map 方法的参数"),e("code",[t._v("callbackfn")]),t._v("不可执行,就报错")]),t._v(" "),e("li",[t._v("如果 map 方法的参数之中,指定了"),e("code",[t._v("this")]),t._v(",就让"),e("code",[t._v("T")]),t._v("等于该参数,否则"),e("code",[t._v("T")]),t._v("为"),e("code",[t._v("undefined")])]),t._v(" "),e("li",[t._v("生成一个新的数组"),e("code",[t._v("A")]),t._v(",跟当前数组的"),e("code",[t._v("length")]),t._v("属性保持一致")]),t._v(" "),e("li",[t._v("如果报错就返回")]),t._v(" "),e("li",[t._v("设定"),e("code",[t._v("k")]),t._v("等于 0")]),t._v(" "),e("li",[t._v("只要"),e("code",[t._v("k")]),t._v("小于当前数组的"),e("code",[t._v("length")]),t._v("属性,就重复下面步骤\n"),e("ol",[e("li",[t._v("设定"),e("code",[t._v("Pk")]),t._v("等于"),e("code",[t._v("ToString(k)")]),t._v(",即将"),e("code",[t._v("K")]),t._v("转为字符串")]),t._v(" "),e("li",[t._v("设定"),e("code",[t._v("kPresent")]),t._v("等于"),e("code",[t._v("HasProperty(O, Pk)")]),t._v(",即求当前数组有没有指定属性")]),t._v(" "),e("li",[t._v("如果报错就返回")]),t._v(" "),e("li",[t._v("如果"),e("code",[t._v("kPresent")]),t._v("等于"),e("code",[t._v("true")]),t._v(",则进行下面步骤\n"),e("ol",[e("li",[t._v("设定"),e("code",[t._v("kValue")]),t._v("等于"),e("code",[t._v("Get(O, Pk)")]),t._v(",取出当前数组的指定属性")]),t._v(" "),e("li",[t._v("如果报错就返回")]),t._v(" "),e("li",[t._v("设定"),e("code",[t._v("mappedValue")]),t._v("等于"),e("code",[t._v("Call(callbackfn, T, «kValue, k, O»)")]),t._v(",即执行回调函数")]),t._v(" "),e("li",[t._v("如果报错就返回")]),t._v(" "),e("li",[t._v("设定"),e("code",[t._v("status")]),t._v("等于"),e("code",[t._v("CreateDataPropertyOrThrow (A, Pk, mappedValue)")]),t._v(",即将回调函数的值放入"),e("code",[t._v("A")]),t._v("数组的指定位置")]),t._v(" "),e("li",[t._v("如果报错就返回")])])]),t._v(" "),e("li",[e("code",[t._v("k")]),t._v("增加 1")])])]),t._v(" "),e("li",[t._v("返回"),e("code",[t._v("A")])])])]),t._v(" "),e("p",[t._v("仔细查看上面的算法,可以发现,当处理一个全是空位的数组时,前面步骤都没有问题。进入第 10 步中第 2 步时,"),e("code",[t._v("kPresent")]),t._v("会报错,因为空位对应的属性名,对于数组来说是不存在的,因此就会返回,不会进行后面的步骤。")]),t._v(" "),e("div",{staticClass:"language-javascript line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-javascript"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("const")]),t._v(" arr "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\narr"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("map")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token parameter"}},[t._v("n")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=>")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n console"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("log")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("1")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// [, , ,]")]),t._v("\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br"),e("span",{staticClass:"line-number"},[t._v("3")]),e("br"),e("span",{staticClass:"line-number"},[t._v("4")]),e("br"),e("span",{staticClass:"line-number"},[t._v("5")]),e("br")])]),e("p",[t._v("上面代码中,"),e("code",[t._v("arr")]),t._v("是一个全是空位的数组,"),e("code",[t._v("map")]),t._v("方法遍历成员时,发现是空位,就直接跳过,不会进入回调函数。因此,回调函数里面的"),e("code",[t._v("console.log")]),t._v("语句根本不会执行,整个"),e("code",[t._v("map")]),t._v("方法返回一个全是空位的新数组。")]),t._v(" "),e("p",[t._v("V8 引擎对"),e("code",[t._v("map")]),t._v("方法的"),e("a",{attrs:{href:"https://github.com/v8/v8/blob/44c44521ae11859478b42004f57ea93df52526ee/src/js/array.js#L1347",target:"_blank",rel:"noopener noreferrer"}},[t._v("实现"),e("OutboundLink")],1),t._v("如下,可以看到跟规格的算法描述完全一致。")]),t._v(" "),e("div",{staticClass:"language-javascript line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-javascript"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("function")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("ArrayMap")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token parameter"}},[t._v("f"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" receiver")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token constant"}},[t._v("CHECK_OBJECT_COERCIBLE")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("this")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Array.prototype.map"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n\n "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Pull out the length so that modifications to the length in the")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// loop will not affect the looping and side effects are visible.")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("var")]),t._v(" array "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token constant"}},[t._v("TO_OBJECT")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("this")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("var")]),t._v(" length "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token constant"}},[t._v("TO_LENGTH_OR_UINT32")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("array"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("length"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("InnerArrayMap")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("f"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" receiver"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" array"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" length"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("function")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("InnerArrayMap")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token parameter"}},[t._v("f"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" receiver"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" array"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" length")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!")]),e("span",{pre:!0,attrs:{class:"token constant"}},[t._v("IS_CALLABLE")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("f"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("throw")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("MakeTypeError")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("kCalledNonCallable"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" f"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n\n "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("var")]),t._v(" accumulator "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("new")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("InternalArray")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("length"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("var")]),t._v(" is_array "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token constant"}},[t._v("IS_ARRAY")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("array"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("var")]),t._v(" stepping "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token constant"}},[t._v("DEBUG_IS_STEPPING")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("f"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("var")]),t._v(" i "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v(" i "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("<")]),t._v(" length"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v(" i"),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("++")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token constant"}},[t._v("HAS_INDEX")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("array"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" i"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" is_array"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("var")]),t._v(" element "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" array"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("i"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Prepare break slots for debugger step in.")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("stepping"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("%")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("DebugPrepareStepInIfStepping")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("f"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n accumulator"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("i"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("%")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("_Call")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("f"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" receiver"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" element"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" i"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" array"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("var")]),t._v(" result "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("new")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("GlobalArray")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("%")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("MoveArrayContents")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("accumulator"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" result"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" result"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])]),t._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[t._v("1")]),e("br"),e("span",{staticClass:"line-number"},[t._v("2")]),e("br"),e("span",{staticClass:"line-number"},[t._v("3")]),e("br"),e("span",{staticClass:"line-number"},[t._v("4")]),e("br"),e("span",{staticClass:"line-number"},[t._v("5")]),e("br"),e("span",{staticClass:"line-number"},[t._v("6")]),e("br"),e("span",{staticClass:"line-number"},[t._v("7")]),e("br"),e("span",{staticClass:"line-number"},[t._v("8")]),e("br"),e("span",{staticClass:"line-number"},[t._v("9")]),e("br"),e("span",{staticClass:"line-number"},[t._v("10")]),e("br"),e("span",{staticClass:"line-number"},[t._v("11")]),e("br"),e("span",{staticClass:"line-number"},[t._v("12")]),e("br"),e("span",{staticClass:"line-number"},[t._v("13")]),e("br"),e("span",{staticClass:"line-number"},[t._v("14")]),e("br"),e("span",{staticClass:"line-number"},[t._v("15")]),e("br"),e("span",{staticClass:"line-number"},[t._v("16")]),e("br"),e("span",{staticClass:"line-number"},[t._v("17")]),e("br"),e("span",{staticClass:"line-number"},[t._v("18")]),e("br"),e("span",{staticClass:"line-number"},[t._v("19")]),e("br"),e("span",{staticClass:"line-number"},[t._v("20")]),e("br"),e("span",{staticClass:"line-number"},[t._v("21")]),e("br"),e("span",{staticClass:"line-number"},[t._v("22")]),e("br"),e("span",{staticClass:"line-number"},[t._v("23")]),e("br"),e("span",{staticClass:"line-number"},[t._v("24")]),e("br"),e("span",{staticClass:"line-number"},[t._v("25")]),e("br"),e("span",{staticClass:"line-number"},[t._v("26")]),e("br"),e("span",{staticClass:"line-number"},[t._v("27")]),e("br"),e("span",{staticClass:"line-number"},[t._v("28")]),e("br")])])])}),[],!1,null,null,null);e.default=n.exports}}]);