我想在一个函数中声明多个变量:
function foo() {
var src_arr = new Array();
var caption_arr = new Array();
var fav_arr = new Array();
var hidden_arr = new Array();
}
这样做是正确的吗?
var src_arr = caption_arr = fav_arr = hidden_arr = new Array();
我想在一个函数中声明多个变量:
function foo() {
var src_arr = new Array();
var caption_arr = new Array();
var fav_arr = new Array();
var hidden_arr = new Array();
}
这样做是正确的吗?
var src_arr = caption_arr = fav_arr = hidden_arr = new Array();
是的,如果你希望它们都指向内存中的同一个对象,则可以这样做,但大多数情况下,您可能希望它们是单独的数组,以便如果其中一个发生变化,其他数组不会受到影响。
如果您不希望它们全部指向同一对象,请执行以下操作
var one = [], two = [];
[]
是创建数组的简写字面量。下面是一个控制台日志,说明了它与其他方式的区别:>> one = two = [];
[]
>> one.push(1)
1
>> one
[1]
>> two
[1]
>> one = [], two = [];
[]
>> one.push(1)
1
>> one
[1]
>> two
[]
在第一部分中,我定义了one
和two
指向在内存中相同的对象/数组。如果我使用.push
方法,它会将1推入数组中,因此one
和two
都有1
。在第二部分中,我为每个变量定义了唯一的数组,所以当我向one
推入时,two
不受影响。请避免使用该赋值模式,即使你想让所有变量都指向同一个对象。
事实上,只有第一个是变量声明,其余的仅仅是对可能存在的未声明标识符进行赋值!
对于未声明的标识符进行赋值(也称为未声明赋值)是不推荐的,因为如果在作用域链中找不到该标识符,则会创建一个全局变量。例如:
function test() {
// We intend these to be local variables of 'test'.
var foo = bar = baz = xxx = 5;
typeof foo; // "number", while inside 'test'.
}
test();
// Testing in the global scope. test's variables no longer exist.
typeof foo; // "undefined", As desired, but,
typeof bar; // "number", BAD!, leaked to the global scope.
typeof baz; // "number"
typeof xxx; // "number"
此外,ECMAScript第5版严格模式禁止这种赋值方式。在严格模式下,对未声明的标识符进行赋值将导致TypeError
异常,以防止隐式全局变量。function test() {
// We correctly declare these to be local variables inside 'test'.
var foo, bar, baz, xxx;
foo = bar = baz = xxx = 5;
}
test();
// Testing in the global scope. test's variables no longer exist.
typeof foo; // "undefined"
typeof bar; // "undefined"
typeof baz; // "undefined"
typeof xxx; // "undefined"
var
语句仅用于foo
标识符,右侧的其余赋值从右到左进行评估,这是由于赋值运算符的结合性所致(想象一下:var foo = value;
其中value
是具有多个赋值(bar = baz = xxx = 5
)的表达式,它们与var
语句无关。)但是,您可以使用逗号来声明多个变量,只需一个var
语句:var one_arr = [],two_arr = [],three_arr = [];
- Christian C. Salvadóvar src_arr = [],
caption_arr = [],
fav_arr = [],
hidden_arr = [];
为了所有目的,应使用literal []
notation syntax。因为Array constructor在处理其parameters时存在歧义。
如果传递给Array构造函数的唯一参数是介于0和232-1(含)之间的整数,则返回一个新的JavaScript数组,其长度属性设置为该数字。这意味着具有传递长度的
Array
为空插槽的数组,其中包含undefined
值。
new Array(1, 2, 3); // Result: [1, 2, 3]
new Array(3); // Result: [empty × 3] with undefined at indexes
new Array('3') // Result: ['3']
//this was ambiguous
let x = new Array(5); // Result: [empty × 5]
x.push("Hello"); //expected x as ["Hello", empty, empty, empty, empty]
//Actual x: [empty × 5, "Hello"]
解构语法:
声明多个变量的另一种更简洁的方法是使用解构赋值,它可以将数组中的值或对象中的属性解包到不同的变量中。
function foo() {
//destructuring assignment syntax
let [src_arr, caption_arr, fav_arr, hidden_arr] = [[], [], [], []];
console.info("Variables::", fav_arr, hidden_arr, caption_arr, src_arr);
fav_arr.push("fav");
hidden_arr.push("hidden");
//After adding some values to couple of arrays
console.info("Variables::", fav_arr, hidden_arr, caption_arr, src_arr);
}
foo();
使用JavaScript声明多个变量的实际上有更短的方式。首先,您可以仅使用一个变量关键字(var、let或const),并通过逗号分隔变量名称和值来声明它们。
const Sam="10yr", Joe="30yr", Bob="15yr", Mary="21yr";
[]
更为简洁。 - meder omuraliev