JavaScript Patterns 3.4 Array Literal
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的更多相关文章
- JavaScript Patterns 3.1 Object Literal
Basic concept Values can be properties: primitives or other objects methods: functions User-defined ...
- JavaScript Patterns 5.3 Private Properties and Methods
All object members are public in JavaScript. var myobj = { myprop : 1, getProp : function() { return ...
- 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 ...
- JavaScript Patterns 6.7 Borrowing Methods
Scenario You want to use just the methods you like, without inheriting all the other methods that yo ...
- JavaScript Patterns 6.5 Inheritance by Copying Properties
Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) ...
- JavaScript Patterns 6.3 Klass
Commonalities • There’s a convention on how to name a method, which is to be considered the construc ...
- JavaScript Patterns 5.5 Sandbox Pattern
Drawbacks of the namespacing pattern • Reliance on a single global variable to be the application’s ...
- JavaScript Patterns 5.4 Module Pattern
MYAPP.namespace('MYAPP.utilities.array'); MYAPP.utilities.array = (function () { // dependencies var ...
- JavaScript Patterns 4.10 Curry
Function Application apply() takes two parameters: the first one is an object to bind to this inside ...
随机推荐
- EPANET能做什么,不能做什么
What Epanet cand and cannot do Good news!Epanet can do most of the calculations you may need for you ...
- SQL Server 2014里的IO资源调控器
在本文中,我们将来看看SQL Server 2014在资源调控器方面增加了哪些新的功能.资源调控器(Resource Governor)是从SQL Server 2008开始出现的一项功能.它是用于管 ...
- Python 的字符串格式化和颜色控制
(部分内容源自武神博客和网络收集.) Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两 ...
- Sprint1(11.20)
Sprint1第一阶段 1.类名:软件工程-第一阶段 2.时间:11.14-11.23 3.选题内容:web版-餐厅到店点餐系统 4.我们详细分析了点餐系统实现的具体功能,分为两种方案: 方案一:此方 ...
- LeetCode - 31. Next Permutation
31. Next Permutation Problem's Link ---------------------------------------------------------------- ...
- 使用jQueryUI的dialog实现一个提示功能
信息提示给用户是程序开发中,最常用的一个功能. Insus.NET使用jQueryUI的dialog来实现一个,可以定义标题,对话框的大小等. 在ASP.NET MVC环境下来演示吧. 在Octobe ...
- 【JS复习笔记】02 对象与函数
好吧,因为很重要的事情,几天没写笔记了. 关于对象: ||可以用来填充默认值,如:myApp.name || "无" &&可以用来避免错误,myApp.NameOb ...
- CSS 最核心的四个概念(摘录)
本文将讲述 CSS 中最核心的几个概念,包括:盒模型.position.float等.这些是 CSS 的基础,也是最常用的几个属性,它们之间看似独立却又相辅相成.为了掌握它们,有必要写出来探讨一下,如 ...
- 那些教程没有的php2-对象
php.net 对象 在类定义内部,可以用 new self 和 new parent 创建新对象. 当把一个对象已经创建的实例赋给一个新变量时,新变量会访问同一个实例,就和用该对象赋值一样.可以用克 ...
- RadioButton 自定义控件
在res/drawable新建radiobutton.xml(本案例为video——evaluate.xml)如下 <?xml version="1.0" encoding= ...