[] 在 JavaScript 中是什么意思? [英] What does [] mean in JavaScript?

查看:28
本文介绍了[] 在 JavaScript 中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的javascript代码中,[]被赋值为一个变量的值,是什么意思?

In the following javascript code there is [] being assigned as the value of a variable, what does it mean?

var openTollDebug = [];

推荐答案

它是一个数组字面量.它与声明 new Array() 并不完全相同 - Array 对象可以在 JavaScript 中被覆盖,但数组文字不能.这里有一个例子来演示

it is an array literal. It is not quite the same as declaring new Array() - the Array object can be overwritten in JavaScript, but the array literal can't. Here's an example to demonstrate

// let's overwrite the Array object
Array = function(id) {
 this.id = id;
}

var a = new Array(1);
var b = [];

console.log(a.hasOwnProperty("id")); // true
console.log(b.hasOwnProperty("id")); // false

console.log(a.push); // false, push doesn't exist on a
console.log(b.push); // true,  but it does on b

b.push(2);
console.log(b); // outputs [2]

这篇关于[] 在 JavaScript 中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆