【发布时间】:2017-11-21 05:25:31
【问题描述】:
为什么说这段代码这两个输出有区别:
var animals = ["a", "b", "c", "d"];
console.log = [4];
console.log(animals.indexOf("e"));
同理,为什么它在 indexOf 处显示 undefined vs -1?
【问题讨论】:
-
在console.log = [4]中,您将控制台对象的日志键分配给数组[4](默认情况下,console.log被分配了一个记录到控制台的函数)
-
animals是一个错字。正确的应该是animal。 -
你使用了两个不同的数组 animal 和 animals
-
你想用
console.log = [4]做什么?你的意思是console.log([4])?
标签: javascript arrays console.log