What's the point of new String(“x”) in JavaScript?

后端 未结 9 1733
走了就别回头了
走了就别回头了 2020-11-30 04:21

What are the use cases for doing new String(\"already a string\")?

What\'s the whole point of it?

相关标签:
9条回答
  • 2020-11-30 05:03

    You can also convert a String object (along with anything else) to a String primitive with toString: var str = new String("foo"); typeof str; // object typeof str.toString(); // string

    0 讨论(0)
  • 2020-11-30 05:04

    There's very little practical use for String objects as created by new String("foo"). The only advantage a String object has over a primitive string value is that as an object it can store properties:

    var str = "foo";
    str.prop = "bar";
    alert(str.prop); // undefined
    
    var str = new String("foo");
    str.prop = "bar";
    alert(str.prop); // "bar"
    

    If you're unsure of what values can be passed to your code then I would suggest you have larger problems in your project. No native JavaScript object, major library or DOM method that returns a string will return a String object rather than a string value. However, if you want to be absolutely sure you have a string value rather than a String object, you can convert it as follows:

    var str = new String("foo");
    str = "" + str;
    

    If the value you're checking could be any object, your options are as follows:

    1. Don't worry about String objects and just use typeof. This would be my recommendation.

      typeof str == "string".

    2. Use instanceof as well as typeof. This usually works but has the disadvantage of returning a false negative for a String object created in another window.

      typeof str == "string" || str instanceof String

    3. Use duck typing. Check for the existence of one or more String-specific methods, such as substring() or toLowerCase(). This is clearly imprecise, since it will return a false positive for an object that happens to have a method with the name you're checking, but it will be good enough in most cases.

      typeof str == "string" || typeof str.substring == "function"

    0 讨论(0)
  • 2020-11-30 05:08

    You could use instanceof if you really want to be paranoid:

    if(typeof x === "string" || x instanceof String)
    

    The instanceof operator will properly handle subclasses of String too:

    obj instanceof ConstructorFunction works by checking if ConstructorFunction.prototype is in the prototype chain of obj.

    I don't think I've ever actually used the String class in JavaScript but there's nothing wrong with being paranoid and aiming for correctness.

    0 讨论(0)
  • 2020-11-30 05:08
    Object.prototype.toString.call(aVariable) == '[object String]'
    
    0 讨论(0)
  • 2020-11-30 05:14

    Thanks All, Even after so many years this question doesn't have a precise answer.

    JavaScript has got two type of data,

    1. Primitives :- string (let a = 'testStr'), number, boolean, null, undefined,symbol and bigint.
    2. Objects :- Everything else (Functions, Arrays, JS Objects, ....)

    Its the way JS is designed for efficiency(you know JS on V8 is like rocket) that all primitives are immutable(changing a str of num creates a new variable behind the scene) and objects are mutable.

    To support primitives to used like objects JS has this feature of AutoBoxing. So when we use any method (say toString() for number) with primitives, JS automatically converts it to the corresponding Object and then executes the method and converts it back to primitive. Normally we should never use the constructor(with new) and use it like a primitive only(let str = 'testStr'). Using constructor object instead of primitive might cause slow execution and complications.

    0 讨论(0)
  • 2020-11-30 05:20

    Why do you need to check if it is string?

    Just check if it is defined or null, and otherwise defensively convert it to any type you want, either the var bar = new String(foo); or var bar = "" + foo;.

    0 讨论(0)
提交回复
热议问题