43

我想在一个函数中声明多个变量:

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();
4

6 回答 6

84

是的,如果您希望它们都指向内存中的同一个对象,但很可能您希望它们成为单独的数组,以便如果其中一个发生变异,其他的不受影响。

如果您不希望它们都指向同一个对象,请执行

var one = [], two = [];

[]是用于创建数组的简写文字。

这是一个显示差异的控制台日志:

>> one = two = [];
[]
>> one.push(1)
1
>> one
[1]
>> two
[1]
>> one = [], two = [];
[]
>> one.push(1)
1
>> one
[1]
>> two
[]

在第一部分中,我定义onetwo指向内存中的同一个对象/数组。如果我使用该.push方法,它会将 1 推到数组中,因此两者onetwo1里面。在第二个中,因为我为每个变量定义了唯一的数组,所以当我推到一个时,两个不受影响。

于 2010-11-02T22:16:10.983 回答
19

即使您想让所有变量都指向同一个对象,也请远离该分配模式。

事实上,只有第一个是变量声明,其余的只是对可能未声明的标识符的赋值!

强烈建议不要为未声明的标识符(也称为未声明的赋值)赋值,因为如果在作用域链上找不到标识符,则会创建一个 GLOBAL 变量。例如:

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 5th Strict Mode 不允许这种分配。在严格模式下,对未声明标识符的赋值将导致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"
于 2010-11-02T22:29:14.690 回答
1

不,您的第二条语句将创建对同一数组的四个引用。你要:

var src_arr     = [],
    caption_arr = [],
    fav_arr     = [],
    hidden_arr  = [];
于 2010-11-02T22:16:00.017 回答
0

所有这些变量都将引用一个 Array 对象。

于 2010-11-02T22:17:23.930 回答
0

出于所有意图和目的,应使用表示法语法。由于Array 构造函数在处理其参数的方式上是模棱两可的。literal []

从文档:

如果传递给 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();

于 2019-10-09T08:43:03.920 回答
-1

实际上有更短的方法来使用 JavaScript 声明多个变量。首先,您只能使用一个变量关键字(var、let 或 const)并声明变量名称和值,并用逗号分隔。

const Sam="10yr", Joe="30yr", Bob="15yr", Mary="21yr";
于 2022-01-27T11:42:16.983 回答