什么是[]在JavaScript中是什么意思? [英] What does [] mean in JavaScript?
本文介绍了什么是[]在JavaScript中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下面的JavaScript code有 []
被指定为变量的值,这是什么意思?
VAR openTollDebug = [];
解决方案
这是文字的数组。这是不太一样的声明新的Array()
- Array对象可在JavaScript中被覆盖,但数组字面不能。下面是一个例子,演示
//让我们覆盖的Array对象
阵列=功能(ID){
this.id = ID;
}VAR一个=新阵列(1);
变种B = [];的console.log(a.hasOwnProperty(ID)); //真
的console.log(b.hasOwnProperty(ID)); //假的console.log(a.push); //假的,推不上存在
的console.log(b.push); //真实的,但它确实在b上b.push(2);
的console.log(B); //输出[2]
In the following javascript code there is []
being assigned as the value of a variable, what does it mean?
var openTollDebug = [];
解决方案
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屋!
查看全文