A javascript object is a compound value, which is a collection of attributes or named values. It references attribute values through the symbol "." When the attribute value is a function, we call it a method. We see that strings also have properties and methods:
The code copy is as follows:
var s="hello,world!";
var word=s.substring(s.indexof("")+1,s.length);
Since a string is not an object, why does it have attributes? As long as the attribute of the string s is referenced, javascript will convert the string value into an object by calling the constructor of new String(s). This object inherits the string method and is used to process the reference to the attribute. Once the attribute reference is completed, the newly created object will be destroyed (in fact, this object will not be created in implementation, but the whole process looks like this).
Like strings, numbers and boolean values also have their own methods: create a temporary object through the Number() and Boolean() constructors, and the calls to these methods are from this temporary object. This temporary object is called a wrapper object.
Notice:
The code copy is as follows:
var s="test"; //Declare a string
s.len=4; //Set a len property for it
var t=s.len; //Query this property
At this time, when we output t, we should undefined. The second line of code creates a temporary string object, and assigns its len attribute value to 4, then destroys the object. The third line of code sets a new attribute through the original string value s and tries to read its len attribute. This attribute naturally does not exist, so the value when t is output is undefined.
This code shows that when reading attribute values (or methods) of numbers, strings, and boolean values, it behaves like an object, but when trying to assign values to its attributes, this operation is ignored: the modification only happens on a temporary object, and this temporary object does not continue to be preserved.
The temporary object created when accessing a property of a string, number, or Boolean is called a wrapper object. It is only occasionally used to distinguish between string values and string objects, numeric objects, Boolean values and Boolean objects.