JavaScript Patterns 3.1 Object Literal
Basic concept
Values can be
properties: primitives or other objects
methods: functions
User-defined native objects are mutable at any time.
Object literal notation is ideal for this type of on-demand object creation.
Even the simplest {} object already has properties and methods inherited from Object.prototype.
var dog = {
name: "Benji",
getName: function () {
return this.name;
}
};
- The Object Literal Syntax
• Wrap the object in curly braces ({ and }).
• Comma-delimit the properties and methods inside the object. A trailing comma after the last name-value pair is allowed but produces errors in IE, so don't use it.
• Separate property names and property values with a colon.
• When you assign the object to a variable, don't forget the semicolon after the closing }.
- Objects from a Constructor
// one way -- using a literal var car = {goes: "far"}; // another way -- using a built-in constructor // warning: this is an antipattern var car = new Object(); car.goes = "far"; - Object Constructor Catch
Don't use new Object(); use the simpler and reliable object literal instead.
// Warning: antipatterns ahead // an empty object var o = new Object(); console.log(o.constructor === Object); // true // a number object var o = new Object(1); console.log(o.constructor === Number); // true console.log(o.toFixed(2)); // "1.00" // a string object var o = new Object("I am a string"); console.log(o.constructor === String); // true // normal objects don't have a substring() // method but string objects do console.log(typeof o.substring); // "function" // a boolean object var o = new Object(true); console.log(o.constructor === Boolean); // true
JavaScript Patterns 3.1 Object Literal的更多相关文章
- JavaScript Patterns 5.7 Object Constants
Principle Make variables shouldn't be changed stand out using all caps. Add constants as static prop ...
- JavaScript Patterns 3.4 Array Literal
Array Literal Syntax To avoid potential errors when creating dynamic arrays at runtime, it's much sa ...
- 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.3 Klass
Commonalities • There’s a convention on how to name a method, which is to be considered the construc ...
- JavaScript Patterns 5.3 Private Properties and Methods
All object members are public in JavaScript. var myobj = { myprop : 1, getProp : function() { return ...
- 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.6 Mix-ins
Loop through arguments and copy every property of every object passed to the function. And the resul ...
- 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.4 Prototypal Inheritance
No classes involved; Objects inherit from other objects. Use an empty temporary constructor function ...
随机推荐
- [Python] raw_input
该函数输入的是字符串,如果想输入数字,可以用强制转换.
- sysbench测试服务器性能
sysbench目前已经有0.5的版本,不过最普遍使用的依旧是0.4.12,所以接下来我们会以0.4.12这个版本作为测试 Step1:下载安装sysbench wget http://pkgs.fe ...
- 检测局域网中还可用的ip地址
#!/bin/bash ` do { .$i &>/dev/null ];then echo "192.168.1.$i is not used" fi } done
- MIUI选项框开关样式模拟
有IOS的开关模拟,当然也有MIUI的开关模拟 看到设置选项里面的开关样式,突发奇想地来试试 最终效果如图: 实现过程 1. 选项框checkbox 模拟开关当然需要一个选项框,这里用到了复选框 ...
- 面向对象的JavaScript(3):私有成员和公开成员
在小项目中对于JavaScript使用,只要写几个function就行了.但在大型项目中,尤其是在开发追求 良好的用户体验的网站中,如SNS,就会 用到大量的JavaScrpt,有时JavaScrip ...
- 在android设备上调试ionic应用
方法1: ionic run android -l -c 将会在console中输出日志信息 方法2: (1).使用usb连接android设备,并打开android设备的调试功能 (2).在chro ...
- 【Jquery回顾】解决$冲突的问题->自定义JQuery快捷键
$(function() { $whatever = jQuery.noConflict(); alert($whatever("#cr").text()); })
- Unity烂笔头1-自定义INSPECTOR属性窗口节点项
1.添加输入框和标签 LevelScript: using UnityEngine; using System.Collections; public class LevelScript : Mono ...
- Python入门笔记(21):Python函数(4):关于函数式编程的内建函数
一.关于函数式编程的内建函数 apply()逐渐被舍弃,这里不讨论 1.filter() #filter(func,seq) """纯Python描述filter函数&q ...
- 【C#进阶系列】03 配置文件管理与程序集的引用版本重定向
先来点与标题不相关的: CLR支持两种程序集:弱命名程序集和强命名程序集. 两者的区别在于强命名程序集使用发布者的公钥和私钥进行签名.由于程序集被唯一性地标识,所以当应用程序绑定到强命名程序集时,CL ...