为什么这段代码不抛出语法错误?
console.log('hello' ['world'])
这两个参数之间应该有一个逗号,但是没有。这不是应该抛出一个语法错误吗?
发布于 2018-11-28 18:14:56
您正在订阅一个字符串( [...]部件被解释为括号表示法而不是数组)。结果将是undefined,因为字符串没有一个名为'world'的属性。
如果下标有效,则结果将是字符串中的一个字符:
console.log('hello'[1]);             // e
其结果可能是取决于所提供的属性的其他内容:
console.log('hello'['toString']);    // logs the function toString of the string 'hello'
console.log('hello'['length']);      // logs the length of the string 'hello'
console.log('hello'['apple']);       // mysteriously logs undefined :)
https://stackoverflow.com/questions/53525656
复制相似问题