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. n 后问题

    n后问题,解决思路:假设每个皇后占一行(且第i个皇后放在第i - 1 行),依次去尝试下一个皇后该放在该行的哪一列 #include<iostream> #include<cmath ...

  2. android resources使用总结

    http://developer.android.com/guide/topics/resources/more-resources.html http://developer.android.com ...

  3. redis主从遇到的两个坑

    最近在使用redis主从的时候做了下面两件事情: 1 希望redis主从从操作上分析,所有写操作都在master上写,所有读操作都在从上读. 2 由于redis的从是放在本地的,所以有的key的读写操 ...

  4. 深度使用react-native的热更新能力,必须知道的一个shell命令

    开篇之前,先讲一个自己开发中的一个小插曲: 今天周日,iOS版 App 周一提交,周三审核通过上架,很给力.不过,中午11:30的时候,运营就反应某个页面有一个很明显的问题,页面没法拉到底部,部分信息 ...

  5. MySQL联接操作

    在MySQL中,联接是一种对表的引用, 多表联接类型: 1.笛卡尔积(交叉联接):在MySQL中为CROSS JOIN或省略JOIN,如: select * from course, teachcou ...

  6. python输出excel能够识别的utf-8格式csv文件

    http://blog.csdn.net/azhao_dn/article/details/16989777 可能大家都遇到过,python在输出的csv文件中如果有utf-8格式的中文,那么在使用e ...

  7. java阻塞队列

    对消息的处理有些麻烦,要保证各种确认.为了确保消息的100%发送成功,笔者在之前的基础上做了一些改进.其中要用到多线程,用于重复发送信息. 所以查了很多关于线程安全的东西,也看到了阻塞队列,发现这个模 ...

  8. 常用jsp include用法,三种include的区别

    <@ include file=””> :静态导入,jsp指令,同一个request , <jsp:include page=”” flush=””>:动作元素,不同一个req ...

  9. php中的常用数组函数(二)(数组元素过滤 array_filter())

    array_filter($arr, 'filter_func'); //参数1,要过滤的数组 //参数2,过滤的函数,返回false时,不添加这个元素,返回true添加这个元素. 示例代码: /** ...

  10. Webhooks PHP

    Webhooks/Parse When webhooks are triggered in the gateway, a notification is sent as a POST request ...