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;
}
};
  1. 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 }.

  2. 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";
  3. 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的更多相关文章

  1. JavaScript Patterns 5.7 Object Constants

    Principle Make variables shouldn't be changed stand out using all caps. Add constants as static prop ...

  2. JavaScript Patterns 3.4 Array Literal

    Array Literal Syntax To avoid potential errors when creating dynamic arrays at runtime, it's much sa ...

  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.3 Klass

    Commonalities • There’s a convention on how to name a method, which is to be considered the construc ...

  5. JavaScript Patterns 5.3 Private Properties and Methods

    All object members are public in JavaScript. var myobj = { myprop : 1, getProp : function() { return ...

  6. JavaScript Patterns 6.7 Borrowing Methods

    Scenario You want to use just the methods you like, without inheriting all the other methods that yo ...

  7. JavaScript Patterns 6.6 Mix-ins

    Loop through arguments and copy every property of every object passed to the function. And the resul ...

  8. JavaScript Patterns 6.5 Inheritance by Copying Properties

    Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) ...

  9. JavaScript Patterns 6.4 Prototypal Inheritance

    No classes involved; Objects inherit from other objects. Use an empty temporary constructor function ...

随机推荐

  1. 文本溢出text-overflow

    文本溢出text-overflow 问题:有一个新闻标题,标题宽度为200px,文字为宋体,加粗,文字大小为16px,颜色为黑色,行高为25px,要求单行显示,并且超出时显示“…”,请按要求完成效果. ...

  2. PHP去除BOM头的方法

    BOM头是UTF-8来告诉编辑器:我是UTF8编码.它的编码是\xEF\xBB\xBF 但是PHP在设计之初并没有考虑到BOM头的问题,所以在编解码的时候很容易出现问题   比如今天遇到的问题,jso ...

  3. 【转】MSSQLServer数据库事务锁机制分析

    锁是网络数据库中的一个非常重要的概念,它主要用于多用户环境下保证数据库完整性和一致性.各种大型数据库所采用的锁的基本理论是一致的,但在具体实现上各有差别.目前,大多数数据库管理系统都或多或少具有自我调 ...

  4. 《深入理解Java集合框架》系列文章

    Introduction 关于C++标准模板库(Standard Template Library, STL)的书籍和资料有很多,关于Java集合框架(Java Collections Framewo ...

  5. [ASP.NET] 结合Web API在OWIN下实现OAuth

    OAuth(Open Authorization) 为用户资源的授权提供了一个安全的.开放而又简易的标准.与以往的授权方式不同之处是OAuth的授权不会使第三方触及到用户的帐号信息(如用户名与密码), ...

  6. HipChat上传文件报未知错误解决方案

    前言 HipChat是Atlassian公司的一款团队协作即时通讯工具,服务端为Linux(官方给的服务端就是一个虚拟机),在Windows.Linux.Android.IOS.Mac等平台都有客户端 ...

  7. 以对象的方式来访问xml数据表(三)

    怎样以对象的方式来访问xml数据表? 在讲如何具体实现(二)中所说的专门用于访问xml文件的动态链接库之前,我们先来看看这个动态链接库具体要实现什么功能. 动态链接库IXmlDB.dll的功能: 1. ...

  8. XmlNodeList循环读取节点值

    foreach (XmlNode item in XmlNodeList) { string oid = item.SelectSingleNode("oid").InnerTex ...

  9. YEdit

    YEdit YEdit is a YAML editor for Eclipse. See the wiki for more details Installation Use the Eclipse ...

  10. java之内的工具分享,附带下载链接,方便以后自己寻找

    class反编译工具:http://pan.baidu.com/s/1geYvX5L redis客户端工具:http://pan.baidu.com/s/1eRJ4ThC mysql客户端-[mysq ...