Array Literal Syntax

To avoid potential errors when creating dynamic arrays at runtime, it's much safer to stick with the array literal notation.

Array Constructor Curiousness

When you pass a single number to the Array() constructor, it doesn't become the value of the first array element.

// an array of one element

var a = [3];

console.log(a.length); //

console.log(a[0]); //

// an array of three elements

var a = new Array(3);

console.log(a.length); //

console.log(typeof a[0]); // "undefined"

// using array literal

var a = [3.14];

console.log(a[0]); // 3.14

var a = new Array(3.14); // RangeError: invalid array length

console.log(typeof a); // "undefined"    

Clever uses of the Array() constructor

var white = new Array(256).join(' '); 

Check for Array-ness

Array.isArray([]); // true

// trying to fool the check with an array-like object

Array.isArray({

    length: 1,

    "0": 1,

    slice: function () {}

}); // false

if (typeof Array.isArray === "undefined") {

    Array.isArray = function (arg) {

        return Object.prototype.toString.call(arg) === "[object Array]";

    };

} 

JavaScript Patterns 3.4 Array Literal的更多相关文章

  1. JavaScript Patterns 3.1 Object Literal

    Basic concept Values can be properties: primitives or other objects methods: functions User-defined ...

  2. JavaScript Patterns 5.3 Private Properties and Methods

    All object members are public in JavaScript. var myobj = { myprop : 1, getProp : function() { return ...

  3. JavaScript Patterns 7.1 Singleton

    7.1 Singleton The idea of the singleton pattern is to have only one instance of a specific class. Th ...

  4. JavaScript Patterns 6.7 Borrowing Methods

    Scenario You want to use just the methods you like, without inheriting all the other methods that yo ...

  5. JavaScript Patterns 6.5 Inheritance by Copying Properties

    Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) ...

  6. JavaScript Patterns 6.3 Klass

    Commonalities • There’s a convention on how to name a method, which is to be considered the construc ...

  7. JavaScript Patterns 5.5 Sandbox Pattern

    Drawbacks of the namespacing pattern • Reliance on a single global variable to be the application’s ...

  8. JavaScript Patterns 5.4 Module Pattern

    MYAPP.namespace('MYAPP.utilities.array'); MYAPP.utilities.array = (function () { // dependencies var ...

  9. JavaScript Patterns 4.10 Curry

    Function Application apply() takes two parameters: the first one is an object to bind to this inside ...

随机推荐

  1. Java sun.misc.Unsafe类的学习笔记

    Java未开源的Unsafe类 Unsafe类可以为我们提供高效并且线程安全方式操作变量,直接和内存数据打交道. 获取Unsafe实体的方法 private static Unsafe getUnsa ...

  2. sed用例

    文件空行处理 1. 在文件中的每一行后面添加一个空行. sed 'G' test.txt 解释: Get命令是将保留空间的内容取出,并添加到当前模式空间的内容之后(添加一行).当保留空间为空时,效果为 ...

  3. 三分 --- POJ 3301 Texas Trip

    Texas Trip Problem's Link:   http://poj.org/problem?id=3301 Mean: 给定n(n <= 30)个点,求出包含这些点的面积最小的正方形 ...

  4. 关于URL、Web的一些概念

    关于URL ★ 书写路径时,网络文件用斜杠“/”划分不同层级,本地文件管理系统用反斜杠“\”,分隔不同层级:                               如下图示   ★  绝对/相对 ...

  5. URL与图像格式

    绝对/相对URL “绝对URL”是指资源的完整的地址,通常以“http://”打头: “相对URL”是指Internet上资源相对于当前页面的地址,它包含从当前页面指向目标资源位置的路径,不以“htt ...

  6. 可访问性级别的C# 修饰符

    使用访问修饰符 public.protected.internal 或 private 可以为成员指定以下声明的访问级别之一. http://keleyi.com/a/bjad/3ccfqh95.ht ...

  7. 与众不同 windows phone 8.0 & 8.1 系列文章索引

    [源码下载] [与众不同 windows phone 7.5 (sdk 7.1) 系列文章索引] 与众不同 windows phone 8.0 & 8.1 系列文章索引 作者:webabcd ...

  8. 与众不同 windows phone (47) - 8.0 其它: 锁屏信息和锁屏背景, 电池状态, 多分辨率, 商店, 内置协议, 快速恢复

    [源码下载] 与众不同 windows phone (47) - 8.0 其它: 锁屏信息和锁屏背景, 电池状态, 多分辨率, 商店, 内置协议, 快速恢复 作者:webabcd 介绍与众不同 win ...

  9. js generator数据类型

    1. 概述 generator 是ES6引入的新的数据类型, 看上去像一个函数,除了使用return返回, yield可以返回多次. generator 由function* 定义, (注意*号), ...

  10. maven oracle jdbc jar

    1.problem describe: when your dependency jar about oracle use code like this: <!-- oracle-connect ...