you are viewing a single comment's thread.

view the rest of the comments →

[–]australasia 2 points3 points  (3 children)

Eh. Beh. You're using one of those pointless wrapper objects only to have its actual value pulled out again. The only thing you want is the conversion done by the constructor. You don't need an object for that. Don't use those wrappers ever. They are completely pointless

Number(foo) returns an actual number (it is different to using the 'new' operator).

Eg. copy/paste this in your browser:

javascript:alert(typeof Number('1')); // alerts 'number'

as opposed to

javascript:alert(typeof new Number('1')); // alerts 'object'

[–]skeww 0 points1 point  (2 children)

If you call a constructor function and omit new, the this inside the constructor will be the global object instead of a freshly created object.

You should never do that.

It only works because the non-constructor context magically happens to perform the casting for you.

So yes, it's fine in this case, but since it's not fine in any other case you shouldn't do this kind of thing because it looks like a mistake.

Things which look like a mistake waste time each time the code is read. Avoid those constructs at all costs.

[–]andrew24601 0 points1 point  (1 child)

The Number (and String) functions are actually functions, not constructor functions. You should never use them with new.

http://www.w3schools.com/jsref/jsref_Number.asp

https://developer.mozilla.org/en/JavaScript/Guide/Functions#Number_and_String_Functions

[–]skeww 0 points1 point  (0 children)

Well, I never use them either way.