new Number() vs Number()

What is the difference between new Number() and Number()? I get that new Number() creates a Number object and Number() is just a function, but when should I call which, and why?

On a related note, Mozilla says:

Do not use a Boolean object to convert a non-boolean value to a boolean value. Instead, use Boolean as a function to perform this task.

x = Boolean(expression);     // preferred
x = new Boolean(expression); // don't use

Why is that? I thought the results were the same?

-----------------------------------------------------------------------------------------------

40down voteaccepted

Boolean(expression) will simply convert the expression into a boolean primitive value, while new Boolean(expression) will create a wrapper object around the converted boolean value.

The difference can be seen with this:

// Note I'm using strict-equals
new Boolean("true") === true; // false
Boolean("true") === true; // true

And also with this (thanks @hobbs):

typeof new Boolean("true"); // "object"
typeof Boolean("true"); // "boolean"

Note: While the wrapper object will get converted to the primitive automatically when necessary (and vice versa), there is only one case I can think of where you would want to use new Boolean, or any of the other wrappers for primitives - if you want to attach properties to a single value. E.g:

var b = new Boolean(true);
b.relatedMessage = "this should be true initially";
alert(b.relatedMessage); // will work var b = true;
b.relatedMessage = "this should be true initially";
alert(b.relatedMessage); // undefined
answered Jan 18 '11 at 0:19
David Tang

71.5k23141134
 

js中有包装类,java中也有包装类的更多相关文章

  1. Java开发学习--Java 中基本类型和包装类之间的转换

    Java 中基本类型和包装类之间的转换 基本类型和包装类之间经常需要互相转换,以 Integer 为例(其他几个包装类的操作雷同哦): 在 JDK1.5 引入自动装箱和拆箱的机制后,包装类和基本类型之 ...

  2. java中String、包装类、枚举类的引用传递

    一般情况下,我们认为Java中了除了八种基本数据类型,其他都是对象,进行引用传递: 但是:String.包装类.枚举类作为参数传递后发现,没有达到引用传递的效果,很多人认为它是值传递! 首先,对象肯定 ...

  3. Java中基本数据和包装类的比较

    public class AutoBoxTest { public static void main(String[] args) { Integer a1 = 127; Integer a2 = 1 ...

  4. Java基础(35):装箱与拆箱---Java 中基本类型和包装类之间的转换(Wrapper类)

    基本类型和包装类之间经常需要互相转换,以 Integer 为例(其他几个包装类的操作雷同哦): 在 JDK1.5 引入自动装箱和拆箱的机制后,包装类和基本类型之间的转换就更加轻松便利了. 那什么是装箱 ...

  5. JAVA中基本类型和包装类之间的相互转换

    转自:https://www.imooc.com/code/2250 仅做个人学习记录之用,侵删. 基本类型和包装类之间经常需要互相转换,以 Integer 为例(其他几个包装类的操作雷同哦): 在 ...

  6. Java 中的包装类

    Java 中的包装类 相信各位小伙伴们对基本数据类型都非常熟悉,例如 int.float.double.boolean.char 等.基本数据类型是不具备对象的特性的,比如基本类型不能调用方法.功能简 ...

  7. Java.lang 包 (包装类、String类、Math类、Class类、Object类)

    Java 的核心 API(Application Programming Interface)是非常庞大的,这给开发者带来了很大的方便. java.lang 包是 Java 的核心类库,它包含了运行 ...

  8. Java中的Html解析:使用jsoup

    包:jsoup-1.10.2.jar import java.io.File; import java.io.IOException; import org.jsoup.Jsoup; import o ...

  9. java 中的常用类

    Java 中的包装类 相信各位小伙伴们对基本数据类型都非常熟悉,例如 int.float.double.boolean.char 等. 基本数据类型是不具备对象的特性的,比如基本类型不能调用方法.功能 ...

随机推荐

  1. webpack-dev-server proxy代理

    一个最简单的代理例子:index.html中有如下代码 fetch('/api/pub/article/list?pageSize=2').then((data)=>{ return data. ...

  2. Mac OS X下安装Vue脚手架(vue-cli)

    前言 Vue作为前端三大框架(Angular,React,Vue)之一,号称是最简单,最容易上手的框架,同时也是行内的大趋势,还可以用来开发最火的小程序.具有开发快,双向数据流等特点,有些人认为Vue ...

  3. 《C/C++专项练习》— (1)

    前言 每每到了一周之计的Monday啊,精神总是不佳,写篇博客提提神儿吧~ 继上次完成<C/C++工程师综合练习卷>后,有事儿没事儿就想刷几道题,赶脚不错,巩固了不少基础知识呢,要坚持哦~ ...

  4. UVa-208 Firetruck (图的DFS)

    UVA-208 天道好轮回.UVA饶过谁. 就是一个图的DFS. 不过这个图的边太多,要事先判一下起点和终点是否联通(我喜欢用并查集),否则会TLE. #include <iostream> ...

  5. cs229_part6

    part 6 接下来就是无监督学习算法了. k均值聚类 问题背景 样本集描述: \[ x\in D, x\in R^n \] 之前的有监督学习问题中,所有的x都有对应的y.但是如果我们的x没有对应的y ...

  6. 使用adb命令启查看已安装的Android应用的versionCode、versionName

    列出已经安装的应用 adb shell pm list package C:\Users\CJTDEV003>adb shell pm list package package:com.sams ...

  7. Centos 6.5升级openssl到1.1.0f版本

    wget https://www.openssl.org/source/openssl-1.1.0f.tar.gz ./config --prefix=/usr/local/openssl share ...

  8. POJ-3481 Double Queue,Treap树和set花式水过!

                                                    Double Queue 本打算学二叉树,单纯的二叉树感觉也就那几种遍历了, 无意中看到了这个题,然后就 ...

  9. [Istio]Web应用出现upstream connect error or disconnect/reset before headers

    在部署web应用之后,使用ingressway为入口,不能正常访问.服务返回 upstream connect error or disconnect/reset before headers 这种情 ...

  10. 九度oj 题目1252:回文子串

    题目描述: 输入一个字符串,输出该字符串中对称的子字符串的最大长度. 比如输入字符串“google”,由于该字符串里最长的对称子字符串是“goog”,因此输出4. 输入: 存在多组数据,每组数据一行字 ...