"6" Basic Package Type
3 Special Reference types: Boolean, number, String.
Whenever a primitive type value is read, the background creates an object of the corresponding basic wrapper type, allowing us to invoke some methods to manipulate the Data.
var New String ("some text"); // Create an instance of type string var s2 = s1.substring (2); // invokes the specified method on the instance null; // Destroy this instance
After this processing, the basic string value becomes the same as the Object.
The main difference between a reference type and a basic wrapper type is the lifetime of the Object. An instance of a reference type created with the new operator is persisted in memory until the execution flow leaves the current scope. Objects that are automatically created by the basic wrapper type exist only in the execution of a single line of code and are immediately destroyed. This means that we cannot add properties and methods to the base type values at Run Time.
var s1 = "some text"/// The second line creates a string object that was destroyed when the third line of code was executed //undefined
Calling typeof on an instance of the base wrapper type returns "object", and all objects of the base wrapper type are converted to Boolean True.
var New Object ("some text"instanceof//true
Using new to invoke the constructor of the basic wrapper type is not the same as a transformation function that calls the same name Directly.
var value = "+"; var // transform function alert (typeof//"number"varnew// Constructor alert (typeof//"object"
(1) Boolean type
The Boolean type is the reference type that corresponds to the Boolean Value. To create a Boolean object, you can call the Boolean constructor and pass in a value of true or False.
var New Boolean (true);
Instances of the Boolean type override the ValueOf () method, return a base type value of TRUE or false, override the ToString () method, and return the string "true" and "false".
var New Boolean (false); var true //true all basic wrapper type objects are converted to Boolean value truevarfalseTrue //false
The typeof operator returns "boolean" to the base type, while the reference type returns "object";
The instanceof operator tests that the Boolean object returns true, whereas a Boolean value that tests the base type returns FALSE.
Alert (typeof//Objectalert (typeof//booleaninstanceof //trueinstanceof//false
(2) Number Type
var New Number (10);
The number type also overrides the valueof (), tolocalstring (), and ToString () methods. The overridden valueof () method returns the number of base types represented by the object, and the other two methods return a numeric value in the form of a string.
In addition to inherited methods, the number type provides methods for formatting numeric values as Strings.
1 toFixed () method: Returns a string representation of a numeric value by the specified number of decimal digits.
var num = ten; alert (num.tofixed (//"10.00"var num1 = 10.005; alert ( Num1.tofixed (//"10.01" Auto Rounding
The toFixed () method can represent a numeric value with 0 to 20 decimal digits. But this is only the scope of the standard implementation, and some browsers may also support more Digits.
2 toexponential () method: Returns the string form of a numeric value expressed in exponential notation (e notation). As with the tofixed () method, the toexponential () method also accepts a parameter, and the parameter is also the number of decimal digits in the specified output Result.
var num = ten; alert (num.toexponential (//"1.0e+1" )
3 The toprecision () method: the fixed-size format may be returned, or the exponential (exponential) format may be returned, depending on which format is most Appropriate. This method takes a parameter, which is the number of digits (excluding the exponential portion) that represents all the digits of a numeric value.
var num =num.toprecision (//"1e+2"//"the" "99.0"
4 "typeof and instanceof operators
var New Number (var numbervalue = ten; alert (typeof//"object"alert ( typeof // "number" instanceof // true instanceof // false
JS Reference type (6)--basic package type