Difference between javascript type string and String object?

I spoke with the ECMA-262 standard (ECMAScript Language Specification, 3rd edition, if that matters), I did not find the difference between the 3rd and 5th edition of String Type / String Object).

There is one thing that puzzles me: the difference between a string type and a string object. Yes, I know the difference in the sense that the String Type is a sequence of 16-bit UTF-16 units, and the String Object is an inline object with its internal Class property set to "String" and its internal value Value, value of type String.

But reading the spec, the string type does not seem to expose any methods; that is, it is simply a value without any additional properties. Take this code, everything is exactly as expected:

document.writeln(typeof "foo"); // 'string' document.writeln(typeof new String("foo")); // 'object' 

The first type is the actual type of the string, and the second is the type of the object (this is an object of class String, but its data type is an object). However, looking at this:

 "foo".charAt(0); fooStrObj = new String("Foo"); fooStrObj.charAt(0); 

Both of them seem to reveal the same functions, but there are no functions in the string type defined in the ECMA-262 standard; all the functions it provides are related to the String.prototype object (and I don’t see a reference to the fact that String Type magically exposes all the properties and functions of the String.prototype object in the ECMA-262 standard). Similarly, String Type values ​​are automatically passed to the String object with the original String value as its internal property Value?

And if they are processed in exactly the same way (which for all goals and tasks they seem to be), why are there two different ways of representing a string?

+59
javascript
Jan 12 '10 at 19:43
source share
2 answers

Strings are a type of value in JS, so they cannot have any properties associated with them without a prototype, etc. Any attempt to access a property on them technically performs a JS [[ToObject]] conversion (essentially a new line).

Easy way to distinguish difference (in browser)

 a = "foo" ab = "bar" alert("ab = " + ab); //Undefined A = new String("foo"); Ab = "bar"; alert("Ab = " + Ab); // bar 

Additionally

 "foo" == new String("foo") 

true, this is true only because of implicit conversions like operator ==

 "foo" === new String("foo") 

will fail.

+39
Jan 12 '10 at 19:54
source share

It is similar to the difference between int and Integer in Java.

According to the standard, when trying to call a method, strings are automatically converted to String objects. See ECMA 262-3, section 11.2.1 ; step 5 calls ToObject (which is defined in section 9.9).

11.2.1 Accessors
[...]
The product of MemberExpression: MemberExpression [Expression] is rated as follows:

  • Evaluate a MemberExpression expression.
  • Call GetValue (Result (1)).
  • Calculate expression.
  • Call GetValue (Result (3)).
  • Call ToObject (Result (2)).
  • Call ToString (Result (4)).
  • Returns a value of type Reference, whose underlying object is Result (5), and whose property name is Result (6).


9.9 ToObject

The ToObject statement converts its argument to a value of type Object according to the following table:
[...]
Create a new String object, the [[value]] property is set to string. See 15.5 for a description of String objects.

As a specification technique, this is a hack to explain how strings may seem to have methods, even if they are not objects.

In addition, wrapper objects are not very useful. I do not know why they are in this language. I rather wish they were not. :)

+12
Jan 12 '10 at 19:47
source share



All Articles