Primitive value types: number, string, boolean, null, and undefined.

// a primitive number

var n = 100;

console.log(typeof n); // "number" 

// a Number object

var nobj = new Number(100);

console.log(typeof nobj); // "object" 

One reason to use the wrapper objects is when you want to augment the value and persist state.  Because primitives are not objects, they  cannot be augmented with properties.

// primitive string

var greet = "Hello there";

// primitive is converted to an object

// in order to use the split() method

greet.split(' ')[0]; // "Hello"

// attemting to augment a primitive is not an error

greet.smile = true;

// but it doesn't actually work

typeof greet.smile; // "undefined" 
When used without new, wrapper constructors convert the argument passed to them to a primitive value:
typeof Number(1); // "number"

typeof Number("1"); // "number"

typeof Number(new Number()); // "number"

typeof String(1); // "string"

typeof Boolean(1); // "boolean"
												

JavaScript Patterns 3.7 Primitive Wrappers的更多相关文章

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

  2. JavaScript Patterns 6.7 Borrowing Methods

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

  3. JavaScript Patterns 6.6 Mix-ins

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

  4. JavaScript Patterns 6.5 Inheritance by Copying Properties

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

  5. JavaScript Patterns 6.4 Prototypal Inheritance

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

  6. JavaScript Patterns 6.3 Klass

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

  7. JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance

    // the parent constructor function Parent(name) { this.name = name || 'Adam'; } // adding functional ...

  8. JavaScript Patterns 6.1 Classical Versus Modern Inheritance Patterns

    In Java you could do something like: Person adam = new Person(); In JavaScript you would do: var ada ...

  9. JavaScript Patterns 5.9 method() Method

    Advantage Avoid re-created instance method to this inside of the constructor. method() implementatio ...

随机推荐

  1. suricata抓包方式之一 AF_PACKET

    1.前言 linux提供了原始套接字RAW_SOCKET,可以抓取数据链路层的报文.这样可以对报文进行深入分析.今天介绍一下AF_PACKET的用法,分为两种方式.第一种方法是通过套接字,打开指定的网 ...

  2. python编码问题的最终分析

    python初学者,往往因为字符编码的问题而苦恼不已,本人也是阅读了大量的博客,再进行了一定的测试,基本搞清楚了编码问题的前因后果.下面一段代码是在python3.5上的,以它为例进行讲解(请忽略糟糕 ...

  3. SpringMVC从Controller跳转到另一个Controller(转)

    http://blog.csdn.net/jackpk/article/details/44117603 [PK亲测] 能正常跳转的写法如下: return "forward:aaaa/bb ...

  4. Linq专题之Linq查询from子句

    Linq查询表达式包含8个常用的子句:from.where.select.join.into.orderby.group.let.我们来看看详细的说明.      from:      指定查询操作的 ...

  5. mysql 判断表字段或索引是否存在,然后修改

    判断字段是否存在: DROP PROCEDURE IF EXISTS schema_change; DELIMITER // CREATE PROCEDURE schema_change() BEGI ...

  6. 循序渐进开发WinForm项目(5)--Excel数据的导入导出操作

    随笔背景:在很多时候,很多入门不久的朋友都会问我:我是从其他语言转到C#开发的,有没有一些基础性的资料给我们学习学习呢,你的框架感觉一下太大了,希望有个循序渐进的教程或者视频来学习就好了. 其实也许我 ...

  7. 将HTML5 Canvas的内容保存为图片

    主要思想是借助Canvas自己的API - toDataURL()来实现,整个实现 HTML + JavaScript的代码很简单. 代码如下: <html> <meta http- ...

  8. Python入门笔记(8):列表

    一.序列类型操作符 1.切片[]和[:] 2.成员关系操作符(in ,not in ) 1: s1 = [1,2,3,4,5,6,7] 2: s2 = [2,3,6] 3: s3 = [] 4: fo ...

  9. table.appand(行数据) datagrid分页

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  10. http 响应码

    一.HTTP码应码响应码由三位十进制数字组成,它们出现在由HTTP服务器发送的响应的第一行. 响应码分五种类型,由它们的第一位数字表示:1.1xx:信息,请求收到,继续处理2.2xx:成功,行为被成功 ...