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. 工作流数据库表设计-ASP.NET

    公司准备开发一套工作流引擎,以前没有什么OA开发经验,也是第一次设计工作流引擎,我把我的一些思路分享一下,希望得到些帮助或者能帮助到一些人. 产品的定位: 1.能够做到前后端分离 2.可以做到项目的分 ...

  2. 2013/11/21工作随笔-PHP开启多进程

    今天被问到一个问题,php如何开启多进程才比较稳定. php开启多进程执行一个操作有哪些方法: 首先想到的是使用pcntl的fork 具体可以参考之前的文章:PHP的pcntl多进程 其次想到的方法是 ...

  3. ActiveMQ学习(三)——MQ的通讯模式

    1) 点对点通讯:点对点方式是最为传统和常见的通讯方式,它支持一对一.一对多.多对多.多对一等多种配置方式,支持树状.网状等多种拓扑结构. 2) 多点广播:MQ适用于不同类型的应用.其中重要的,也是正 ...

  4. office2016 软件全集 官方下载免费完整版(含破解文件)不含垃圾软件 win10完美激活

    office2016官方下载免费完整版是新一代办公软件,office2016官方下载免费完整版已经分享到下面,office2016官方下载免费完整版包括了Word.Excel.PowerPoint.O ...

  5. C# WinFrom 跨线程访问控件

    1.跨线程访问控件委托和类的定义 using System; using System.Windows.Forms; namespace ahwildlife.Utils { /// <summ ...

  6. C#创建服务及使用程序自动安装服务,.NET创建一个即是可执行程序又是Windows服务的exe

    不得不说,.NET中安装服务很麻烦,即要创建Service,又要创建ServiceInstall,最后还要弄一堆命令来安装和卸载. 今天给大家提供一种方式,直接使用我们的程序来安装/卸载服务,并且可以 ...

  7. 不可或缺 Windows Native (4) - C 语言: 预处理命令,输入,输出

    [源码下载] 不可或缺 Windows Native (4) - C 语言: 预处理命令,输入,输出 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 预处理命令 输入 ...

  8. 不可或缺 Windows Native (10) - C 语言: 文件

    [源码下载] 不可或缺 Windows Native (10) - C 语言: 文件 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 文件 示例cFile.h #ifn ...

  9. sql server:compare data from two tables

    --Comparing data between two tables in SQL Server --Create two Tables-- CREATE TABLE TableA(ID Int, ...

  10. 基于Eclipse的Go语言可视化开发环境

    http://jingyan.baidu.com/article/d7130635032e2f13fdf475b8.html 基于Eclipse的Go语言可视化开发环境 | 浏览:2924 | 更新: ...