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. EPANET能做什么,不能做什么

    What Epanet cand and cannot do Good news!Epanet can do most of the calculations you may need for you ...

  2. mysql 行锁一则

       CREATE TABLE `t1` (  `id` int(11) NOT NULL DEFAULT '0',  `name` varchar(20) DEFAULT NULL,  PRIMAR ...

  3. SpringMVC核心——参数获取与Servlet资源获取问题

    一.SpringMVC 使用 @PathVariable.@RequestParam.@RequestHeader.@CookieValue 等来解决参数获取问题. 1. @PathVariable: ...

  4. sprint个人总结+读书博客

    读书感想:    第8章讲了需求分析,在我的日常软件编写中,肯定需要需求分析的,一个没有需求的软件,编写出来也没有什么意义,只能是丢在一个角落里发霉.需求有各种各样,要怎样才能满足客户的需求呢,那就要 ...

  5. Linux各版本的本地root密码破解方法

    (一)RedHat/CentOS/Fedora 系统密码破解 1.在grub选项菜单按E进入编辑模式 2.编辑kernel 那行最后加上S (或者Single) 3.按B,启动到single-user ...

  6. Linq之group子句

    在Linq查询语句中,group子句主要作用是对查询的结果集进行分组.并返回元素类型为IGrouping<TKey,TElement>的对象序列. 下面我们在代码实例中创建一个GroupQ ...

  7. 在node.js中使用COOKIE

    node.js中如何向客户端发送COOKIE呢?有如下两个方案: 一.使用response.writeHead,代码示例: //设置过期时间为一分钟 var today = new Date(); v ...

  8. mssql server提示无权限

    mssqlserver在查询系统视图时(如:select * from sys.syscacheobjects),有时会报出如下提示: 消息 300,级别 14,状态 1,第 1 行VIEW SERV ...

  9. [moka同学笔记]linux服务器防火墙的设置

    网站突然打不开:服务器停止了,重启后,防火墙自动启动,导致网站打不开. 1.查看防火墙 systemctl status firewalld 2.关闭防火墙 systemctl stop firewa ...

  10. 数组json格式的字符串 转 list<Bean>

    1.  字符串形式: [ { "userid": "admin", "name": "admin", "pas ...