JavaScript, Basic Package Type

Basic Package Type

  In order to facilitate operation of the basic data types, JavaScript also provided three special reference types: String / Number The / Boolean .

   Look at the following piece of code:

var s1 = "Hello world!!!";
var s2 = s1.substring(5);

   We want to know, s1 is a basic type, base type is no way, only the objects have properties and methods. Then have a question, why you can call substring () methods?

    The reason: When you call s1.substring (5) when the first s1 will be packaged into a temporary object of type String, and then calls the substring () method, and finally the destruction of temporary objects.

   Equivalent to:

was s1 = new String ( 'Hello World !!!'); 
was s2 = s1.substring (5); 
s1 = null;

  

Creating Basic package type:

var num = 18; // numerical basic types 
var num = Number ('18 ') ; // cast 
var num = new Number (18) ; // basic package type, the object

 

Note : String most commonly used packaging, Number, and Boolean types of basic need basic package, use of the words may be ambiguous. 

  Demo:

var b1 = new Boolean(false);
var b2 = b1 && true;

 

  Let's discuss what value b2 is about?

  b2 = true。

  Reason: B1 is a Boolean object, its original value (primitiveValue) is false. b1 && true because b1 is a non-empty object, it will default conversion to true. Therefore, the value of b2 to true .

  Extensions : converted to five cases of false: 0 '' null NaN undefined 

 

Guess you like

Origin www.cnblogs.com/niujifei/p/11367594.html