最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

language features - What is the difference between "new Number(...)" and "Number(...)" in Ja

programmeradmin1浏览0评论

In Javascript, one of the reliable ways to convert a string to a number is the Number constructor:

var x = Number('09'); // 9, because it defaults to decimal

Inspired by this question, I started wondering — what is the difference between the above and:

var x =new Number('09');

Number certainly looks better, but it seems like a slightly inappropriate use of a constructor. Are there any side effects or any difference to using it without the new? If there is no difference, why not, and what is the purpose of new?

In Javascript, one of the reliable ways to convert a string to a number is the Number constructor:

var x = Number('09'); // 9, because it defaults to decimal

Inspired by this question, I started wondering — what is the difference between the above and:

var x =new Number('09');

Number certainly looks better, but it seems like a slightly inappropriate use of a constructor. Are there any side effects or any difference to using it without the new? If there is no difference, why not, and what is the purpose of new?

Share Improve this question edited May 23, 2017 at 10:29 CommunityBot 11 silver badge asked Mar 4, 2010 at 17:32 NicoleNicole 33.2k11 gold badges77 silver badges102 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 14

In the first case, you are using the Number Constructor Called as a Function, as described in the Specification, that will simply perform a type conversion, returning you a Number primitive.

In the second case, you are using the Number Constructor to make a Number object:

var x = Number('09');
typeof x; // 'number'

var x = new Number('09');
typeof x; // 'object'

Number('1') === new Number('1'); // false

The difference may be subtle, but I think it's important to notice how wrapper objects act on primitive values.

Number returns a primitive number value. Yeah, it's a bit odd that you can use a constructor function as a plain function too, but that's just how JavaScript is defined. Most of the language built-in types have weird and inconsistent extra features like this thrown in.

new Number constructs an explicit boxed Number object. The difference:

typeof Number(1)      // number
typeof new Number(1)  // object

In contrast to Java's boxed primitive classes, in JavaScript explicit Number objects are of absolutely no use.

I wouldn't bother with either use of Number. If you want to be explicit, use parseFloat('09'); if you want to be terse, use +'09'; if you want to allow only integers, use parseInt('09', 10).

SpiderMonkey-1.7.0:

js> typeof new Number('09');
object
js> typeof Number('09');
number

Number (without new) doesn't seem to result exactly in a primitive. In the following example the anyMethod() is called (if in the Number prototype).

Number(3).anyMethod()

Whereas

3.anyMethod()

will not work.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论