Home > Article > Web Front-end > Why a='abc' is not equal to a=new String('abc')
This article mainly introduces why a="abc" is not equal to a=new String("abc"). Friends who need it can refer to it. I hope it can help everyone.
Obvious
a="abc" typeof a //string b=new String("abc") typeof b // object a==b //true a===b //false
But why? I read a lot of books and asked several experts, but I was actually still confused. Record it here for future reference.
In js, distinguish between original data type and packaging type. Numbers, strings, Boolean, null, and undefined are primitive data types, while Number, String, and Boolean are packaging types. What is created through new Number is a derived object of the packaging type. So the two are not equal.
The usage process after direct assignment to the basic type is as follows:
1. Create an instance of the String type
2. Call the specified value on the instance Method
3. Destroy the instance
Example:
var a="123" a.toFixed===Number.prototype.toFixed;
There is another saying here : Boxing, unboxing
Boxing is to use this value class to construct a corresponding packaging object
var a=10 ,b="javascript" , c=true; var o_a=new Number(a); var o_b=new String(b); var o_c=new Boolean(c);
The biggest role of boxing is to handle values as objects.
Unboxing is to convert the packaging object into a value type
var a=10; var o_a=new Number(a); var b=o_a.valueOf();//这就是拆箱的过程。
Related recommendations:
ajax gets php Return parameters of the page, control assignment method
Sharing about jquery dynamic assignment id and dynamic id retrieval method
How to give js in php Array assignment
The above is the detailed content of Why a='abc' is not equal to a=new String('abc'). For more information, please follow other related articles on the PHP Chinese website!