See this question today, as follows:
var yiifaa = ‘yiifaa‘, str1 = new String(yiifaa), str2 = String(yiifaa)
3 variables are declared and assigned, then the questions are as follows:
// 请确认以下的判断是否准确str1 === yiifaa//str2 === yiifaa//typeof str1 === typeof str2
According to the syntax of JS, to meet the conditions of = = = as follows:
1. If it is a reference type, then two variables must point to the same object (the same address);
2. If it is a base type, the values must be equal, except that the type must be the same for the two variables.
Then switch the topic to the string object, there are three ways to declare the string (see the first code), but the resulting type is different, the result is as follows:
// 类型为string,为基本类型typeof yiifaa// 类型为object,为引用类型typeof str1// 类型为string,为基本类型typeof str2
Now the answer is clear, as follows:
// false, 因为str1为引用类型str1 === yiifaa// true, 因为都是基本类型,并且值相等str2 === yiifaa// false, 虽然都是字符串,但分别为object与stringtypeof str1 === typeof str2
Summarize
Very magical string object, on the one hand can be as the basic type, on the other hand can also be used as reference type, more amazing is, for String.prototype Add method, basic type can also refer to, how to do?
The difference between a string () and a new string () in JS