Skip to content

Instantly share code, notes, and snippets.

@roshanca
Last active April 10, 2017 03:05
Show Gist options
  • Save roshanca/f17958a1810bcdea9d4ba50950a7dac4 to your computer and use it in GitHub Desktop.
Save roshanca/f17958a1810bcdea9d4ba50950a7dac4 to your computer and use it in GitHub Desktop.
清空数组两种方式 #tag: array
var a = [1, 2, 3];
var b = a;
a = [];
console.log(a);    // []
console.log(b);    // [1,2,3]

a = [] 方式清除 a 变量数组引用其变量 b 的数组不变

var c = [1, 2, 3];
var d = c;
c.length = 0;
console.log(c);    // []
console.log(d);    // []

c.length = 0 可将引用其变量 d 的数组一起清空

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment