preface

As we all know, JS data types fall into two categories: primitive (primitive) types and reference types.

Values of primitive types are simple data segments held in stack memory that are accessed by value. JS has six basic types: Undefined, Null, Boolean, Number, String, and Symbol.

The value of a reference type is an object stored in heap memory whose value is accessed by reference. The main reference types are Object, Array, Function, RegExp, and Date.

Object type

Objects have properties and methods like this:

Const people = [' yellow ',' red '] people.push(' blue ') people.length // 3Copy the code

Array is a reference type, so it naturally has properties and methods. But when we declare a string, we can call the substring method. Why?

const word = 'you are the best'
const subWord = word.substring(0, 7) // "you are"
Copy the code

String is a primitive type. Why should we have methods? It’s all because of something called the basic packaging type.

Basic packing type

In addition to the Object, Array, and other reference types mentioned earlier, JavaScript provides three special reference types: String, Number, and Boolean so that we can manipulate the corresponding basic types.

The value of word itself does not change, even though it uses the substring method. Calling this method simply returns a new string. This is where the basic packaging type comes in. Originally you are not method, but you want to use method, you just tune, corresponding to the basic packaging type has this method on the line. For example, the substring method above does not have a method of the primitive string type, but the string wrapper type does. It will execute the method and return the result. Many things happen when the following line of code is executed:

 word.substring(0, 7)
Copy the code

First, it reads word values from memory, and while it’s in this read mode, the background gets to work. JS high-level programming describes these actions completed in the background as follows:

  1. createStringType
  2. Invokes the specified method on the instance
  3. Destroy the instance

The above code can be interpreted as:

const _word = new String('you are the best') const subWord = _word.substring(0, 7) // "you are" _word = nullCopy the code

So, instead of the primitive string executing its own method, a corresponding primitive wrapper type string is created behind the scenes, which instantiates an instance of the primitive value, calls the specified method, and destroys itself.

Due to the “destroy” nature of primitive wrapper types, we cannot add custom properties and methods for primitive type values.

const word = 'you are the best'
word.name= 'john'
console.log(word.age) //undefined
Copy the code

We added the name attribute to Word. However, this attribute cannot be accessed again because: On the second line of code, an instance of the base wrapper type is created in the background. The attribute is attached to the instance, but then the instance is destroyed. On the third line, the new instance of the base wrapper type is created again without the name attribute.

The display uses the basic wrapper type

In addition to helping us create primitive wrapper type instances while the string is in read mode, we can also create them explicitly ourselves.

const _word = new String('you are the best')
const subWord = _word.substring(0, 7) // "you are"
Copy the code

Print on the console and it looks like this:

However, this is different from what is stored in variables when we create objects in the background.

const word = 'you are the best'
const _word = new String('you are the best')
typeof word // 'string'
typeof _word // 'object'
Copy the code

conclusion

Having a basic wrapper type makes it easier to manipulate the three basic types: String, Boolean, and number. When the values of the three basic types are read, an instance of the corresponding wrapper type is created in the background, which calls the specified method and is destroyed. This short life cycle also means that we cannot add custom properties and methods for base types.