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 ...
随机推荐
- 关于开发Windows服务程序容易搞混的地方!
在开发Windows服务程序时,我们一般需要添加安装程序,即:serviceInstaller,里面有几个关于名称属性,你都搞明白了吗? 1.Description:表示服务说明(描述服务是干什么的) ...
- IOS高级编程之二:IOS的数据存储与IO
一.应用程序沙盒 IOS应用程序职能在系统为该应用所分配的文件区域下读写文件,这个文件区域就是应用程序沙盒.所有的非代码文件如:图片.声音.映象等等都存放在此. 在mac中command+shift+ ...
- 组合数学 - 母函数的运用 + 模板 --- hdu : 2082
找单词 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 脊柱外科病人资料管理系统的界面设计分析(2)--JOA评分记录的实现
在上篇随笔<脊柱外科病人资料管理系统的界面设计分析>中介绍了一些常用的界面设计方面的内容,本篇继续上一篇,介绍脊柱外科病人管理系统的JOA评分记录模块的界面设计以及实现方面的内容. JOA ...
- 关于重装系统与Visual Studio 2015
开始入坑 ... 9.25左右,由于本人电脑上同时安装了社区版vs2013与社区版vs2015,.无奈天公不作美,vs2015我每次一点击右上角的“登录”,马上就白屏,并弹窗提示,停止运行(忘了截屏 ...
- BackgroundWorker实现的winfrom中实现异步等待加载图片显示
BackgroundWorker简介 BackgroundWorker在winfrom中有对应控件,该有三个事件:DoWork .ProgressChanged 和 RunWorkerCompl ...
- DataSet与DataTable对象
DataSet与DataTable对象 摘自:http://www.cnblogs.com/fttbfttb/articles/1509662.html DataSet对象 DataSet是ADO.N ...
- RSA密钥——JAVA与C#的区别和联系
PS:好久没写博了,最近在考虑以后的事情,而且手上杂事也比较多,终于得空来写两篇. 首先感谢:http://www.codeproject.com/Articles/25487/Cryptogra ...
- MySQL预处理语句
预制语句的SQL语法基于三个SQL语句: PREPARE stmt_name FROM preparable_stmt; EXECUTE stmt_name [USING @var_name [, @ ...
- json format validator
http://la5u.org/archives/542 http://stedolan.github.io/jq/download/ https://linuxtoy.org/archives/jq ...