[英]JavaScript array `push` with square brackets instead of parentheses - no error?
我偶然做到了這一點......
var numbers = [1, 2, 3, 4];
numbers.push[5];
為什么沒有錯誤信息?
推需要括號,而不是方括號。 這只是一個簡單的錯字。 我沒有對我正在做的事情給予足夠的關注......但為什么沒有錯誤信息呢?
據我所知,數字數組沒有以任何方式修改。 它只是...沒有。
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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.