码迷,mamicode.com
首页 > Web开发 > 详细

js Array vs [],以及是否为空的判断

时间:2017-08-20 15:40:59      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:div   href   arrays   ons   with   and   基本   bsp   between   

两者基本相同,唯一不同点在于初始化:

var a = [],            // these are the same
b = new Array(),   // a and b are arrays with length 0

c = [‘foo‘, ‘bar‘],           // these are the same
d = new Array(‘foo‘, ‘bar‘),  // c and d are arrays with 2 strings

// these are different:
e = [3]             // e.length == 1, e[0] == 3
f = new Array(3),   // f.length == 3, f[0] == undefined

也就是说Array(arg),其中的arg是指生成数组的长度。

参考:What’s the difference between “Array()” and “[]” while declaring a JavaScript array?

 

检查array是否为空:

if (array === undefined || array.length == 0) {
    // array empty or does not exist
}

 

js Array vs [],以及是否为空的判断

标签:div   href   arrays   ons   with   and   基本   bsp   between   

原文地址:http://www.cnblogs.com/drizzlewithwind/p/7238496.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!