9

Possible Duplicate:
Why are there two kinds of JavaScript strings?

For example, we need to use new RegExp() instead of the regex literal if we need the regex expression to be dynamically calculated.

However exactly what are the edge cases when anyone will ever need to use String/Number/Boolean objects as opposed to their primitive versions ? (because I can't seem to even think of one where it will ever be needed)

Community
  • 1
  • 1
Pacerier
  • 86,231
  • 106
  • 366
  • 634

1 Answers1

1

A String is an Object, but the primitive version exists which is created as a literal with 'Hello' (and is by far the most common used).

People sometimes use new String() to convert another type to a String, for example, in a function.

function leadingZero(number, padding) {
   number = new String(number);
   ...
}

Leading 0s are not significant in a Number, so it must be a String.

However, I still would have preferred to have made the Number a String by concatenating it with an empty String ('').

function leadingZero(number, padding) {
   number += '';
   ...
}

This will implicitly call the toString() of the Number, returning a String primitive.

I was reading that people say hey typeof foo==="string" is not fool-proof because if the string is created using new String the typeof will give us object.

You can make a fool proof isString() method like so...

var isString = function(str) {
   return Object.prototype.toString.call(str) == '[object String]'; 
}

jsFiddle.

This works in a multi window environment. You could also check the constructor property, but this fails in a multi window environment.

Also refer to Felix Kling's comments to this answer.

Community
  • 1
  • 1
alex
  • 479,566
  • 201
  • 878
  • 984
  • 1
    You can also convert to a string by calling the `toString()` method directly. – Felix Kling Aug 09 '11 at 23:59
  • yep that's what i'm saying. is there any benefit in using `new String` as compared to concatenation with "" ? otherwise why would people even have a reason to use `new String` *at all* ? – Pacerier Aug 10 '11 at 00:00
  • @Felix You are correct, I believe concatenating with a string will implicitly call that method (`toString()`) on non `String`s. – alex Aug 10 '11 at 00:01
  • @Pacerier Not really. I've always stuck with the primitives. There are also `new Number()`, `new Boolean()`, `new Array()`, etc. The primitive or literal versions are always preferred except for edge cases. – alex Aug 10 '11 at 00:02
  • 1
    @Pacerier: Using the constructor functions for these data types is discouraged because of exactly these troubles they could cause. There is really hardly a reason why you should use them. If you do see them used somewhere, it is likely that whoever wrote the code has not a good understanding of JavaScript. – Felix Kling Aug 10 '11 at 00:05
  • @alex what are the edge cases? (actually that exactly is my question, but i didn't know how to phrase it until you have phrased it) – Pacerier Aug 10 '11 at 00:08
  • @Felix take a look at the edited question. if you say there are hardly any reasons why we would use them, you do mean that there will still be reasons that we will use them right? what exactly are those reasons? – Pacerier Aug 10 '11 at 00:09
  • 1
    @Pacerier For example, `new RegExp()` must be used if you need to the regex as a string (to possibly concatenate portions of another string). For the `String` object, I'm not sure what edge cases would exist for *anyone* to use the constructor over the primitive. One difference is the `String` constructor can have additional properties set on the Object whilst the primitive can't. [jsFiddle](http://jsfiddle.net/alexdickson/MrFrL/). – alex Aug 10 '11 at 00:11
  • @alex nice point about the RegExp. i've incorporated it into the question to make the question clearer – Pacerier Aug 10 '11 at 00:21
  • @Pacerier OK, your question seems to have a wider scope then it was originally so I have voted to reopen it. Do you want a list of edge cases per Object, or just to know that *yes, there are certain edge cases where the constructor is necessary, don't entirely dismiss the Object constructors*? – alex Aug 10 '11 at 00:29
  • @alex yes a list of edge cases per Object would be cool, because simply *"yes, there are certain edge cases where the constructor is necessary"* without evidence is not convincing at all.. – Pacerier Aug 10 '11 at 02:54