js basic package Type Details

Basic Package Type

The basic types of packaging Boolean, Number and type of string, each time a primitive type value is read, the background will create a type of the object corresponding to the basic package.

Logically, the basic value is not the type of the object, there is no way but the technical point of view, when a primitive type value is read, the background automatically runs the following procedure:

  • Creating a basic package type Boolean, Number, an instance of type String
  • Calls on the specified instance method
  • Examples of the type of destruction of the basic package

The above process may be thought of as the following code:

var s1 = new String("some text");// Boolean, Number 也是如此
var s2 = s1.substring(2);
s1 = null;

While this can create an instance of a basic package types technically, but in the case of non-essential, do not do it.

Because typeof returns "object", and all kinds of examples of the basic package will be converted to a boolean value true for instance of the base type of packaging called.

The main difference between reference types and type of packaging is basic survival instance:

  • References created using the new operator of the type of instance, have been stored in memory before leaving the current scope in the execution flow.
  • Automatic Background instance of the base type of packaging created, there will be only one line of code, and therefore can not add properties and methods of the basic package types at runtime
var s1 = "some text";
s1.color = "red";
console.log(s1.color);// undefined

Examples of using the Object constructor to create the basic package type

var obj = new Object("some text");
console.log( obj instanceof String );// true
var obj = new Object(25);
console.log( obj instanceof Number );// true
var obj = new Object(true);
console.log( obj instanceof Boolean );// true

Guess you like

Origin www.cnblogs.com/zxcv123/p/12026697.html