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. SSL握手步骤【收藏】

    http://www.codeweblog.com/ssl-handshake-process-of-interaction-and/ SSL to send a message in the fol ...

  2. SQL语句技巧:查询时巧用OR实现逻辑判断

    首先看以下SQL逻辑语句块: ) ) SET @fieldname='chassisno' --这里可传入chassisno,plateno,owner,contacttelno其中之一或不传 SET ...

  3. 转载:全球首个微信小程序(应用号)开发教程!通宵吐血赶稿,每日更新!

    微信应用号(小程序,「应用号」的新称呼)终于来了! 目前还处于内测阶段,微信只邀请了部分企业参与封测.想必大家都关心应用号的最终形态到底是什么样子?怎样将一个「服务号」改造成为「小程序」? 我们暂时以 ...

  4. Requested registry access is not allowed(不允许所请求的注册表访问权)

    尝试创建自定义事件日志时,将会收到“Requested registry access is not allowed(不允许所请求的注册表访问权)”错误消息 EventLog.CreateEventS ...

  5. JMS学习(一)基本概念

    这两天面试了一两个公司,由于简历中的最近一个项目用到了JMS,然而面试官似乎对这个很感兴趣,所以都被问到了,但可惜的是,我除了说我们使用了JMS外,面对他们提出的一些关于JMS的问题,我回答得相当差, ...

  6. [moka同学摘录]在Centos 6.5下成功安装和配置了vim7.4

    来源:https://my.oschina.net/gzyh/blog/266097 资源下载地址: 链接:http://pan.baidu.com/s/1kVuaV5P 密码:xkq9   摘要: ...

  7. IBATIS动态SQL(转)

    直接使用JDBC一个非常普遍的问题就是动态SQL.使用参数值.参数本身和数据列都是动态SQL,通常是非常困难的.典型的解决办法就是用上一堆的IF-ELSE条件语句和一连串的字符串连接.对于这个问题,I ...

  8. 转收藏:Git常用命令速查表

    一. Git 常用命令速查 git branch 查看本地所有分支git status 查看当前状态 git commit 提交 git branch -a 查看所有的分支git branch -r ...

  9. 默认选中ComboBox的某一项

    如: 让它选中“统计今天”(控件Name为cobListTime) 方法: 1.cobListTime.Text = cobListTime.Items[0].ToString();//默认选中第一个 ...

  10. C++之内联函数与constexpr

    inline 函数 规模小,流程直接且频繁调用 cout<<shortString(s1,s2)<<endl; = cout<<(s1.size()<s2.s ...