First, we need to understand the original concept of worth
Original value
Simple data segments stored in the stack, that is, their values are stored directly at the location where the variable is accessed.
Reference value
Objects stored in the heap, that is, the value stored at the variable is a pointer pointing to the memory where the object is stored
―――――The concept in w3c was referenced
The original value, to be simple, is null undefined string number Boolean
Converting objects to boolean is relatively simple
All objects (including arrays and functions) are converted to true, and the wrapper object is also an object, and it is also converted to true.
The book says this: "The temporary objects created when accessing properties of strings, numbers and booleans are called wrapper objects." This is how I understand it. The new string, number, and boolean are all considered wrapper objects. They are different from objects, but they are indeed objects. The main difference is that they cannot define new attributes for wrapper objects, because the attributes of string number Boolean are read-only.
Convert object to string type
If the object has the toString() method, call the toString() method. If a primitive value is returned, convert the original value into a string, and the object is converted into this string. If the toString() method is not available or the value returned by this method is not an original value, call the valueOf() method. In the same routine, if the return is an original value, convert the original value into a string, and the object is converted into this string. If the return is not an original value, a type conversion error will be thrown.
The toString() method and valueOf() method here will not be described one by one.
Convert object to number type
Compared with the process of converting to string and converting number, it is just the opposite. First, call the valueOf() method, then call the toString() method. Finally, the toString() method returns not the original value, and js throws an error.
When using the "==" and "+" operators for numerical calculations or string splicing, if one side of the operator is an object, a special object is converted to the original value is used. For non-date objects, converting objects into original values is
The object calls the valueOf() method first, then the tostring() method, and directly converts the original value obtained by calling these two methods as the original value into the object. For a date object, first call the toString() method, then call the valueOf method.
For converting date objects to original values, give an example
var now=new Date(); typeOf(now+1); //"string" converts the date object into a string, because the toString() method typeOf(now-1); //"number" reflects the flexibility of js, "-" converts the string into a number
The above method of converting objects into original values is all the content I have shared with you. I hope you can give you a reference and I hope you can support Wulin.com more.