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. 【转】Python中的GIL、多进程和多线程

    转自:http://lesliezhu.github.io/public/2015-04-20-python-multi-process-thread.html 目录 1. GIL(Global In ...

  2. android 获取当前位置

    1. Android开发位置感知应用程序方式:1. GPS 定位     精确度高,仅适用于户外,严重消耗电量.如果手机内置GPS接受模块,即使手机处于信号盲区,依然可以获取位置信息. 2. NETW ...

  3. How to remove replication in SyteLine V2

    以前曾经写了一篇<How to remove replication in Syteline>http://www.cnblogs.com/insus/archive/2011/12/20 ...

  4. WPF关闭应用程序方法

    很多人认为关闭应用程序应该很简单,例如WindowsForm里一个Application.Exit();方法就可以解决问题,但在WPF里面可别滥用,因为WPF里Application类没有该方法,倒是 ...

  5. imfong.com,我的新博客地址

    imfong.com新博客采用jekyll+Github搭建,欢迎访问.

  6. 用stimulsoft Reports报表工具制作简单报表的过程

    这是在数据库sql server中People表的数据

  7. MySQL更新优化

    通常情况下,当访问某张表的时候,读取者首先必须获取该表的锁,如果有写入操作到达,那么写入者一直等待读取者完成操作(查询开始之后就不能中断,因此允许读取者完成操作).当读取者完成对表的操作的时候,锁就会 ...

  8. 2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest, B. Layer Cake

    Description Dasha decided to bake a big and tasty layer cake. In order to do that she went shopping ...

  9. fibonacci数列从a到b的个数

    Description 我们定义斐波那契数列如下: f1=1 f2=2 f(n)=f(n-1)+f(n-2)(n>=3)   现在,给定两个数a和b,计算有多少个斐波那契数列中的数在a和b之间( ...

  10. git version 2.5.0.windows.1中文乱码问题解决方案

    UI部分 Options->Text Local:zh_CN,Character set:GBK ~/.GitConfig [gui] encoding = utf-8 [tgit] proje ...