The original link to my blog.

Why is “JavaScript everything is an object”? Is this “everything” really “everything”?

It’s not.

There are seven main types of JavaScript:

  • String
  • Number
  • Boolean
  • Null
  • Undefined
  • Symbol
  • Object

Note that typeof NULL returns Object, which is actually a bug. Null is the value of a primitive type.

Typeof NULL // Object principle: In JavaScript, the first three bits of a binary are all zeros, so the first three bits of a null binary are all zeros, so “Object” is returned when typeof is executed.

Based on JavaScript’s classification of language types, it’s easy to know that not everything in JavaScript is an object, or that anything that isn’t a basic type is an object type.

Base type, reference type (object type)

Distinguish basic type, object type, in the final analysis or from the basic type, object type of their own characteristics.

Basic type: Including six of the above, the value of a basic type is a simple data segment. Stored in stack memory; When copying a value of a base type, it copies its value (changing one, leaving the other unaffected), and when comparing, compares by its value.

Reference type: A data structure that is the properties and methods of a class of objects; A value of a reference type is an object that may consist of multiple values and is stored in stack memory and heap memory. When copying a value of a reference type, its reference is copied (changing one, the other changes accordingly) and compared by its reference.

Base types and reference types are easily distinguished by their respective characteristics.

  • Can add/remove attributes

      // Reference type
      let arr = []
      arr.name = 'jk'
      arr.name // jkCopy the code
      // Basic type
      let str = 'mark'
      str.name = 'chao'
      str.name // undefinedCopy the code
  • After copying, change one of the variables to see if the other variable is affected

    • When a value of a primitive type is copied, a new value is created and then copied to the location allocated for the new variable, after which the two variables do not affect each other.

        let a = 1
        let b = a
        b = 2
       console.log(a) / / 1Copy the code

      Copy before:

      After the copy:

    • When a value of a reference type is copied, it also copies the value stored in the variable into the space allocated for the new variable, except that the value is actually a pointer to the heap memory. At the end of the copy, both variables reference the same object, changing one and changing the other.

         let obj1 = {
           name: 'Mary'
         }
      
         let obj2 = obj1
      
         obj2.name = 'Jack'
         console.log(obj1.name) // 'Jack'Copy the code

Basic wrapping function

Since primitive types are not objects and therefore have no properties or methods, why use methods like length and charAt? What comes into play here is the basic wrapper function.

Every time a primitive type value is read, an object of the corresponding primitive wrapper type is created behind the scenes, allowing us to call methods to manipulate the data (null, undefined has no constructor form).

A simple example:

let str = 'Jack'
let oStr = str.substring(2)Copy the code

In the second line of code, STR is accessed in read mode, that is, the value of the string is read from memory, and the following steps are performed:

  • Create an instance of String;
  • Invoke the corresponding method on the instance.
  • Destroy the instance.

In another form:

let str = new String('Jack')
let oStr = str.substring(2)
str = nullCopy the code

Basic packaging function, and the main difference between a reference type is the lifetime of an object, use the new operator to create instances of a reference type, the execution flow before leaving the scope has been kept in memory, and automatically create basic packing type of object,, only exist with a single line of code executed in an instant, and then be destroyed immediately. This is why you can’t add properties and methods to primitive types.


To the end.