结果是["1",undefined *2],因为map只能被初始化过的数组成员调用. The result is["1", undefined × 2], as map is only invoked for elements of the Array which have been initialized. 有意思的是,在JavaScript里,null和undefined 其实是不一样的,所以我们会看到两个不同的错误消息。 Interestingly, in JavaScript, null and undefined are not the same, which is why we see two different error messages. 在JavaScript中,共有6种基本数据类型:string,number,boolean,null,undefined ,symbol(ECMAScript2015新增)。 In JavaScript, there are 6 primitive data types: string, number, boolean, null, undefined , symbol(new in ECMAScript 2015). 常用方法利用了这一事实:如果某个参数没有给定值,那么就是简单undefined 。 The common approach is by leveraging the fact that if an argument is not given a value, then it's simply undefined . 因此,在打印变量的时候,它在函数中存在(它被声明了),但它仍然是undefined 。 So, at the time the variable a is printed, it exists in the function(it's declared) but it's still undefined .
这就是为什么如果你在实际声明之前尝试访问其中的一个变量,就会得到undefined 。 That's why if you try access one of those variables before it was actually declared, you will just get undefined . 每个作为其独特的类型的唯一成员,undefined 是Undefined 类型和null是Object类型。 Each serve as the sole member of its own distinct type; undefined is of type Undefined and null is of type Null. 执行上面的代码会导致以下错误:“UncaughtTypeError:undefined isnotafunction”。 Executing the above code results in the following error:"Uncaught TypeError: undefined is not a function.". Undefined (值)是一个原始值,是未定义类型的基础值。An undefined (value) is a primitive and is the sole value of the undefined type.Undefined 和null在意义上的差异是JavaScript的设计的意外,大部分时间它们的差异是无关紧要的。The difference in meaning between undefined and null is an accident of JavaScript's design, and it doesn't matter most of the time. 但是,在你自己写代码时:显式地将缺失的元素声明为undefined ,将大大提高你的代码的清晰度和可维护性。 However, when writing your own code, explicitly declare the missing elements as undefined , as doing this increases your code's clarity and maintainability. Undefined 和null之间的意义差异是JavaScript设计的一个意外,大多数时候它并不重要。The difference in meaning between undefined and null is an accident of JavaScript's design, and it doesn't matter most of the time. Null/undefined会被转化为数字:null被转化为0,undefined 被转化为NaN。 Null/undefined are converted to numbers: null becomes 0, while undefined becomes NaN. 注:这和内置的isNaN不同,内置的当碰到非数字值(如undefined )时也返回true. Note: this is not the same as the native isNaN function, which will also return true for many other not-number values, such as undefined . 因此对它赋新值不会改变类型undefined 的值。 Trying to assign a new value to it will not alter the value of the undefined . 请注意null和undefined在这有点不同:null变成数字0,undefined 变成NaN。 Null/undefined are converted to numbers: null becomes 0, while undefined becomes NaN. 因为它是一个外部API,试图获取一个不存在的元素返回一个null值,而不是undefined 。 Because it is an external API, attempting to retrieve a non-existent element returns a null value rather than undefined . 注意,上面带有y的例子是有效的,因为x已经初始化(为隐式默认值undefined )了。 Notice, that previous example from above with y is valid, since x is already initialized(to implicit default value undefined ). 译理解JavaScript中的undefined与其他的语言相比,JavaScript中undefined 的概念是有些令人困惑的。 Compared to other languages, JavaScript's concept of undefined is a little confusing. 请注意null和undefined在这有点不同:null变成数字0,undefined 变成NaN。 Please note that null and undefined behave differently here: null becomes zero while undefined becomes NaN.
Display more examples
Results: 297 ,
Time: 0.0222