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. Java 中文字符判断 中文标点符号判断

    Java Character 实现Unicode字符集介绍  CJK中文字符和中文标点判断 主要内容: 1. Java Character类介绍: 2. Unicode 简介及 UnicodeBloc ...

  2. Python 的字符串格式化和颜色控制

    (部分内容源自武神博客和网络收集.) Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两 ...

  3. IOS开发UI基础UISlide属性

    UISlide属性 •    minimumValue  : 当值可以改变时,滑块可以滑动到最小位置的值,默认为0.0_slider.minimumValue = 10.0; •    maximum ...

  4. mysqldump命令的常用组合

    只导表结构完整语句: mysqldump -h192.168.1.174  --port=3306 -uroot -p  --routines --events --no-data  --no-cre ...

  5. 如何转移数据库MDF和LDF文件

    我们可以很轻易地使用SQL Server来创建一个数据库,创建的数据库实例将存储在指定的默认位置(不一定是C盘,可以手动变更默认存储位置).假设此时数据库实例创建在了C盘中的默认位置,亦即是与数据库安 ...

  6. 第二个冲刺 Sprint

    顺带 MY—HR 成员: 角色分配 学号 博客园 团队贡献分 丘惠敏 PM项目经理 201406114203 http://www.cnblogs.com/qiuhuimin/ 19 郭明茵 用户 2 ...

  7. JS 将一段文本 每个英文首字母大写

    function replaceStr(str){ // 正则法 str = str.toLowerCase(); var reg = /\b(\w)|\s(\w)/g; // \b判断边界\s判断空 ...

  8. Insus Search Utility Ver2

    一个搜索组件,虽然不是很强大,但它到现在为止,已经是第二个版本了.前一版本:http://www.cnblogs.com/insus/archive/2011/03/30/1999759.html此版 ...

  9. vs 2010 中类文文件模板的修改

    类模板 文件的修改,以前也修改过,这次有个同事问我,搞了有一会才搞定,这里还是记录分享下. 如果想在每次创建文件时,自动生成文档注释(注意是自动生成文档注释而不是帮助文档),如下面的代码,需要设置VS ...

  10. sql: table,view,function, procedure created MS_Description in sql server

    --添加描述 geovindu --https://msdn.microsoft.com/en-us/library/ms180047.aspx --https://msdn.microsoft.co ...