JavaScript type System--wrapper object

Source: Internet
Author: User
Tags object object true true

xTable of contents [1] definition [2] lifetime [3] explicitly created [4] transformation function [5] before comparison operation

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 ' Hello World ' ;    Console.log (s.length); //  One

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

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

var s1 = ' some text '; var s2 = s1.substring (2); // The above process looks like a three step var New // (1) Create an instance  of type string var // (2) Invoking the specified method on the instance NULL // (3) destroying this instance

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 '= ' red '; alert (s1.color); // undefined
var New Boolean (' Some text '= ' 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 New Object (' abc '); var New Object (true); var New Object (123);

"2" constructor mode

var New String (' abc '); var New Boolean (true); var 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 New String (s); var New Object (s); Console.log (typeof S,typeof S1,typeof S2,typeof S3) ; // string String Object Object

Comparison operation

The

"1" equals the operator ' = = ' treats the original value and its wrapper object as equal, because one of the operands is an object that needs to call the object's valueof () method, the Number object, the Boolean object, and the valueof () of the String object method returns its corresponding original value

var New String (' abc '); var s2 = ' abc '; var New Number (123); var n2 = 123; var New Boolean (true); var true  = = S2, N1 = = N2, B1 = = B2); //  True True True

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

var New String (' abc '); var s2 = ' abc '; var New Number (123); var n2 = 123; var New Boolean (true); var true  = = = S2, N1 = = = N2, B1 = = = B2); //  false False

Resources

"1" JavaScript Advanced Programming (3rd Edition), chapter 3rd basic Concept 5th Chapter reference type
"2" JavaScript Definitive Guide (6th edition) 3rd Chapter type, value, and variable

JavaScript type System--wrapper object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.