带有方括号而不是括号的JavaScript数组`push` - 没有错误?

Vin*_*nce 6 javascript arrays

我偶然做到了这一点......

var numbers = [1, 2, 3, 4];
numbers.push[5];
Run Code Online (Sandbox Code Playgroud)

为什么没有错误信息?

需要括号,而不是方括号.这只是一个简单的错字.我没有对我正在做的事情给予足够的关注......但为什么没有错误信息呢?

据我所知,数字数组没有以任何方式修改.它只是...没有.

Dev*_*gay 9

numbers.push只是一个函数,但你试图找到位于5它的键的属性,它将评估为undefined.

function test() {
  console.log("test");
}


// test[5] evaluates to `undefined` and does nothing
console.log(test[5]);

// We can even manually set this without messing up the function
test[5] = "foo";

// outputs "foo"
console.log(test[5]);

// outputs our expected value "test"
test();
Run Code Online (Sandbox Code Playgroud)