Home  >  Article  >  Web Front-end  >  Why a="abc" is not equal to a=new String("abc")

Why a="abc" is not equal to a=new String("abc")

小云云
小云云Original
2018-01-03 10:58:521882browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn