Define a string

At work there are approximately 3 ways to define a string: 1. var STR = 'hello'; 2. var str1 = String('hello'); 3. var str2 = new String('hello'); (The above three variables are directly taken.... below) Each of the three methods defines 'hello' with its own attributes such as lengh and its own methods such as indexOf(), and does not feel any difference in the day-to-day working definition. 1. Is the definition of 'hello' the same in these three ways? 2. Why can primitive types call their corresponding methods directly?Copy the code

Are these three definitions of ‘hello’ the same?

Console. log(STR === str1) //true console.log(STR === str2) //false console.log(str1 === str2) //false We can find the last way to define it Is not equal to the two definitions above. ????? The first thing we know is that new must be an object. So type three types:  console.log(typeof str) //string console.log(typeof str1) // string console.log(typeof str2) //object So that's why it's not strictly equal.Copy the code

Introduces the relationship between data types and stacks

String,Number, and Boolean are basic types in JS. Basic types are stored in the stack memory, and the size of the data is determined. A = {num:1}, b=a, b.num1 = 2, and a.num1 = 2. Because a and B both point to the same address. The first two methods define a pointer on the stack with equal values, while the third method defines only a pointer on the stack. So that's why the three definitions are different.Copy the code

Why can primitive types call their corresponding methods directly?

Try: console.log(str.length) // 5 str.say = 'world' console.log(str.say) //undefined console.log(str1.lengh) // 5 str1.say = 'world' console.log(str1.say) //undefined console.log(str2.lengh) // 5 str2.say = 'world' console.log(str.say) //worldCopy the code

Leads to wrapper objects and raw data types

We found that both the first and second methods can access lengh attributes, but why can't we customize a property and access it? Primitives: New String() and New Number() are all primitives. Wrapped objects are also objects. That's why we print three types: String (raw data type), String (raw data type), and Object (wrapped object). IndexOf === string.prototype.indexof) // true Then try the first method and the second method Is there the same true? console.log(str.indexOf === String.prototype.indexOf) //true console.log(str1.indexOf === String.prototype.indexOf) Str1 instanceof String str1 instanceof String????? Because: This is JS design. This is design in JS. This is design in JS. The methods and properties of a primitive data type are "borrowed" primitive data type values. Objects have no properties or methods. Primitive data types use properties and methods that are "borrowed" from the enclosing object. So primitive data types can borrow all methods from new String() or new Number(). But it has no properties or methods of its own. So that's why the first one and the second one we can't customize but we can use methods of the corresponding type, rightCopy the code

Conclusion:

1. The first and second methods define the raw data type and store it on the stack, and wrap the object (new.. 2. The third way is to wrap the object, storing the heap pointer in the stack and the content in the heap. So this is also a series of seemingly abnormal but the cause of the normal things, ha ha ha ha ha ha ha ha. Of course, there are a lot of things, since the involvement of the stack, then have to understand what a stack is, what is the difference and so on.Copy the code