A JavaScript object is a composite value that is a collection of properties or named values. Through the '. ' Symbol to reference the property value. When a property value is a function, it is called a method. The method in object o is called by O.M (). We find that strings also have properties and methods
var s = ' Hello World '; Console.log (s.length);//11
Since the string is not an object, why does it have attributes? This leads to the content of today's introduction-The packaging object
Defined
In JavaScript, "Everything is Object", even three primitive types of values (numeric, String, Boolean), under certain conditions, are automatically converted to objects, that is, the original type of "wrapper object"
The wrapper object is a special reference type. The temporary object that is created is called a wrapper object whenever a string, number, or Boolean property or method is read
To make it easier to reference the properties and methods of a string, JavaScript converts the string value into an object by calling the new string (), which inherits the properties and methods of the string and is used to handle the property and the method reference. Once a property or method reference is finished, the newly created object is destroyed. Numbers or Boolean values are similar
[note] This temporary object is not necessarily created or destroyed, but the whole process looks like this
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>
var S1 = ' some text '; var s2 = s1.substring (2);//The above process seems to occur three steps var S1 = new String (' some text '); (1) Create an instance of string type var s2 = s1.substring (2); (2) Call the specified method on the instance S1 = null; (3) Destroying this instance
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>
Survival time
The primary 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 kept 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 you cannot add properties and methods to the base type value at run time
var S1 = ' some text '; s1.color = ' red '; alert (s1.color);//undefined
var s2 = new Boolean (' some text '); s2.color = ' red '; alert (s2.color);//' Red '
Explicitly create an
You can create a wrapper object by using the new operation modifier explicitly, but you should do so if absolutely necessary. Because of this, it's easy to tell if you're dealing with a basic type or a value of a reference type.
There are two ways to explicitly create a wrapper type:
"1" Object mode
var s = new Object (' abc '); var b = new Object (true); var n = new Object (123);
"2" constructor mode
var s = new String (' abc '), var b = new Boolean (true), var n = new number (123);
Transformation functions
Calling a transform function directly is not the same as a constructor that uses new to call the basic wrapper type, and the transformation function returns the base type value
var s = ' abc ', var S1 = string (s), var s2 = new String (s), var s3 = new Object (s); Console.log (typeof s,typeof S1,typeof s2,ty Peof S3);//string String Object Object
Comparison operation
The "1" equals operator ' = = ' treats the original value as equal to its wrapper object because one of the operands is an object that needs to call the valueof () method of the object, and the Number object, the Boolean object, and the ValueOf () method of the string object return their corresponding original values
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>
var S1 = new String (' abc '), var s2 = ' abc ', var n1 = new Number (123), var n2 = 123;var B1 = new Boolean (true); var b2 = True;c Onsole.log (S1 = = S2, N1 = = N2, B1 = b2);//true true True
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>
The "2" equality operator ' = = = ' treats the original value and its wrapper object as unequal. Because the congruent operator wants to compare types and values, the original value differs from the type of its wrapper object
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>
var S1 = new String (' abc '), var s2 = ' abc ', var n1 = new Number (123), var n2 = 123;var B1 = new Boolean (true); var b2 = True;c Onsole.log (S1 = = = S2, N1 = = = N2, B1 = = B2);//false false
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;float:left;border:none; "/>
JavaScript type System-Wrapper object