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. DirectWrite文字排版——字符串去尾

    DirectWrite是 DirectX 家族中专门用来做文本处理的部分,主要配合Direct2D进行渲染工作. 一.字符串去尾介绍 在文字渲染中,不免会遇到字符串去尾的需求.字符串去尾指的是:当字符 ...

  2. 利用session做国际化引起的old区内存爆满及修复方法

    题记:昨天加班打车回家,看见前面有辆路虎在高速上开的巨慢,挡住了我坐的出租车的路,于是就跟司机吐槽了一句:“前面这车怎么这么面啊?”,司机沉默了大概3秒,说了一句富含哲理性的话:“没有面车,只有面人” ...

  3. ok6410 android driver(11)

    This essay, I go to a deeply studying to android HAL device driver program. According to the android ...

  4. 面向对象的JavaScript(3):私有成员和公开成员

    在小项目中对于JavaScript使用,只要写几个function就行了.但在大型项目中,尤其是在开发追求 良好的用户体验的网站中,如SNS,就会 用到大量的JavaScrpt,有时JavaScrip ...

  5. MVC+EF+esayui初试(一 布局加菜单显示)

    最近都在做linq+ext.net的开发.这两天想学习下MVC和ef,刚好,在看ext.js的时候也喜欢上了esayui,所以就想用mvc+ef+esayui做一个汽车网后台管理来加强下.在这里也把我 ...

  6. 用C#开发的双色球走势图(二)

    昨晚由于时间的原因只写了一部分内容,今天将这一部分内容补充完毕,多谢各位园友的支持. 这是用C#开发的双色球走势图(一)新的园友可以看昨晚写的内容,以免脱节.首先回复园友的评论,有说好的有说不好的,本 ...

  7. 004_URL 路由 - 定制路由系统 & 使用区域

    定制路由系统 路由系统是灵活可配置的,当然还可以通过下面这两种方式定制路由系统,来满足其他需求. 1.  通过创建自定义的RouteBase实现: 2.  通过创建自定义路由处理程序实现. 创建自定义 ...

  8. LeetCode128:Longest Consecutive Sequence

    题目: Given an unsorted array of integers, find the length of the longest consecutive elements sequenc ...

  9. PHP框架Swoole的一个定时器Timer特性

    在各种业务型系统中,往往需要服务器在后台扫描相关数据,触发相应的统计.通知等操作. 比如对于一个项目管理系统,需要每天的特定时间内,统计每项任务的执行.到期情况.整个项目的进度等等,根据统计情况,做相 ...

  10. c语言笔试题(带答案)

    填空: 1,short int a[10]={123, 456, 789}; sizeof(a)= 对于64位机来说,指针为8字节表示.其中 sizeof是一运算符,返回编译器为其分配的数组空间大小, ...