Below I will bring you an implementation method for converting objects into primitive values. Let me share it with you now and give it as a reference for everyone.
First of all, we need to understand the concept of primitive value
Primitive value
Simple data stored in the stack segments, that is, their values are stored directly at the location where the variable is accessed.
Reference value
An object stored in the heap, that is, the value stored in a variable is a pointer ( point), pointing to the memory location where the object is stored
————Refers to the concept in w3c
The original value, a simple understanding is null undefined string number Boolean these
Converting objects to boolean is relatively simple
All objects (including arrays and functions) are converted to true, and packaging objects are also objects and are also converted to true
In the book This is what it says: "The temporary objects created when accessing properties of strings, numbers, and Boolean values are called packaging objects." This is how I understand it. Strings, numbers, and booleans generated by new are all considered packaging objects. , it is different from an object, but it is indeed an object. The main difference is that new attributes cannot be defined for the wrapped object, because the attributes of string number Boolean are read-only.
Convert the object to string type
If the object has a toString() method, call the toString() method. If an original value is returned, the original value is Convert to a string, the object is converted to this string, if it does not have a toString() method or the value returned by this method is not a primitive value, then call the valueOf() method, the same routine, if it returns a primitive value , the original value is converted into a string, and the object is converted into the string. If the returned value is not a primitive value, a type conversion error is thrown.
The toString() method and valueOf() method here will not be described one by one.
The object is converted to the number type
Comparison conversion To string, the process of converting number is exactly the opposite. First call the valueOf() method, then call the toString() method. Finally, if the toString() method returns not an original value, js will throw an error.
When using the "==" and " " operators to perform numerical calculations or string concatenation, if one side of the operator is an object, a special method of converting the object into a primitive value will be used. For non-date objects, converting the object into a primitive value means that the object first calls the valueOf() method, then calls the tostring() method, and converts the original value obtained by calling these two methods directly as an object. Original value. For date objects, call the toString() method first, and then call the valueOf method.
For converting date objects into primitive values, let’s give an example.
var now=new Date(); typeOf(now+1); //"string" 将日期对象转换成了字符串,因为先调用的是toString()方法 typeOf(now-1); //"number" 体现了js的灵活性,"-"将字符串转换线成了number
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
Detailed explanation of the five common functions in JavaScriptDetailed explanation of code optimization for javascriptJavaScript call and applyThe above is the detailed content of Implementation method of converting objects into primitive values (graphic tutorial). For more information, please follow other related articles on the PHP Chinese website!