What is the difference between string primitives and String objects in JavaScript?

前端 未结 12 1530
孤街浪徒
孤街浪徒 2020-11-22 07:58

Taken from MDN

String literals (denoted by double or single quotes) and strings returned from String calls in a non-constructor context (i.e., witho

相关标签:
12条回答
  • 2020-11-22 08:36

    The existence of an object has little to do with the actual behaviour of a String in ECMAScript/JavaScript engines as the root scope will simply contain function objects for this. So the charAt(int) function in case of a string literal will be searched and executed.

    With a real object you add one more layer where the charAt(int) method also are searched on the object itself before the standard behaviour kicks in (same as above). Apparently there is a surprisingly large amount of work done in this case.

    BTW I don't think that primitives are actually converted into Objects but the script engine will simply mark this variable as string type and therefore it can find all provided functions for it so it looks like you invoke an object. Don't forget this is a script runtime which works on different principles than an OO runtime.

    0 讨论(0)
  • 2020-11-22 08:40

    The biggest difference between a string primitive and a string object is that objects must follow this rule for the == operator:

    An expression comparing Objects is only true if the operands reference the same Object.

    So, whereas string primitives have a convenient == that compares the value, you're out of luck when it comes to making any other immutable object type (including a string object) behave like a value type.

    "hello" == "hello"
    -> true
    new String("hello") == new String("hello") // beware!
    -> false
    

    (Others have noted that a string object is technically mutable because you can add properties to it. But it's not clear what that's useful for; the string value itself is not mutable.)

    0 讨论(0)
  • 2020-11-22 08:41

    JavaScript has two main type categories, primivites and objects.

    var s = 'test';
    var ss = new String('test');
    

    The single quote/double quote patterns are identical in terms of functionality. That aside, the behaviour you are trying to name is called auto-boxing. So what actually happens is that a primitive is converted to its wrapper type when a method of the wrapper type is invoked. Put simple:

    var s = 'test';
    

    Is a primitive data type. It has no methods, it is nothing more than a pointer to a raw data memory reference, which explains the much faster random access speed.

    So what happens when you do s.charAt(i) for instance?

    Since s is not an instance of String, JavaScript will auto-box s, which has typeof string to its wrapper type, String, with typeof object or more precisely s.valueOf(s).prototype.toString.call = [object String].

    The auto-boxing behaviour casts s back and forth to its wrapper type as needed, but the standard operations are incredibly fast since you are dealing with a simpler data type. However auto-boxing and Object.prototype.valueOf have different effects.

    If you want to force the auto-boxing or to cast a primitive to its wrapper type, you can use Object.prototype.valueOf, but the behaviour is different. Based on a wide variety of test scenarios auto-boxing only applies the 'required' methods, without altering the primitive nature of the variable. Which is why you get better speed.

    0 讨论(0)
  • 2020-11-22 08:42

    we can define String in 3-ways

    1. var a = "first way";
    2. var b = String("second way");
    3. var c = new String("third way");

    // also we can create using 4. var d = a + '';

    Check the type of the strings created using typeof operator

    • typeof a // "string"
    • typeof b // "string"
    • typeof c // "object"


    when you compare a and b var a==b ( // yes)


    when you compare String object

    var StringObj = new String("third way")
    var StringObj2 = new String("third way")
    StringObj  == StringObj2 // no result will be false, because they have different references
    
    0 讨论(0)
  • 2020-11-22 08:47

    In case of string literal we cannot assign properties

    var x = "hello" ;
    x.y = "world";
    console.log(x.y); // this will print undefined
    

    Whereas in case of String Object we can assign properties

    var x = new String("hello");
    x.y = "world";
    console.log(x.y); // this will print world
    
    0 讨论(0)
  • 2020-11-22 08:49

    This is rather implementation-dependent, but I'll take a shot. I'll exemplify with V8 but I assume other engines use similar approaches.

    A string primitive is parsed to a v8::String object. Hence, methods can be invoked directly on it as mentioned by jfriend00.

    A String object, in the other hand, is parsed to a v8::StringObject which extends Object and, apart from being a full fledged object, serves as a wrapper for v8::String.

    Now it is only logical, a call to new String('').method() has to unbox this v8::StringObject's v8::String before executing the method, hence it is slower.


    In many other languages, primitive values do not have methods.

    The way MDN puts it seems to be the simplest way to explain how primitives' auto-boxing works (as also mentioned in flav's answer), that is, how JavaScript's primitive-y values can invoke methods.

    However, a smart engine will not convert a string primitive-y to String object every time you need to call a method. This is also informatively mentioned in the Annotated ES5 spec. with regard to resolving properties (and "methods"¹) of primitive values:

    NOTE The object that may be created in step 1 is not accessible outside of the above method. An implementation might choose to avoid the actual creation of the object. [...]

    At very low level, Strings are most often implemented as immutable scalar values. Example wrapper structure:

    StringObject > String (> ...) > char[]
    

    The more far you're from the primitive, the longer it will take to get to it. In practice, String primitives are much more frequent than StringObjects, hence it is not a surprise for engines to add methods to the String primitives' corresponding (interpreted) objects' Class instead of converting back and forth between String and StringObject as MDN's explanation suggests.


    ¹ In JavaScript, "method" is just a naming convention for a property which resolves to a value of type function.

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