JavaScript objects-An explanation of the basic packaging type

Source: Internet
Author: User
Tags function prototype

I roughly classify JavaScript objects as "reference type", "Basic wrapper type" and "Built-in object" three pieces of content according to the contents of the book.

Let's take a look at the detailed usage of the basic packaging type first!

First, we have to solve the relevant concepts:

1. A value (object) of a reference type is an instance of a reference type.

In ECMAScript, a reference type is a data structure used to organize data and functionality together. It is also commonly referred to as a class.

Although Ecmasript is technically an object-oriented language, it does not have the basic structure of classes and interfaces supported by traditional object-oriented languages. Reference types are also sometimes referred to as object definitions because they describe the properties and methods that a class of objects have.

To facilitate the operation of the basic data types, ECMAScript also provides 3 special reference data types: Boolean, number, and string.

In practice, every time we read a basic data value, the background creates a corresponding basic wrapper type object, which allows us to invoke some methods to manipulate the data.

As in the following example:

var S1 = "some text";

var s2 = s1.substring (2);

Guess what kind of things the backstage will do, go, let's take a look:

1, an instance of string type;

2. Invoke the specified method in the instance;

3. Destroy this instance.

The above three steps can be imagined as executing the following Ecmastript code.

var S1 = new String ("some text");

var s2 = s1.substring (2);

S1 = null;

  The difference between a reference type and a basic wrapper type:

They have different object life cycles:

      • Reference type: Use new to create an instance of a reference type that is stored in memory when the execution data flow leaves the current scope.
      • Basic wrapper Type: An object that automatically creates a basic wrapper type, which is destroyed immediately after a single line of code is executed.

This means that adding properties and methods to the base wrapper type value at run time is not valid.

Second, walk, together learn the basic packaging types of detailed usage.

1. Boolean Boolean Object type

The Boolean object is used to convert a value that is not a Boolean type to a Boolean value (True or false).

1.1 Creating a Boolean object

A Boolean object represents two values: "True" or "false"

The following code defines a Boolean object named Myboolean

var Myboolean = new Boolean ();

If the Boolean object has no initial value or its value is:

        • 0
        • -0
        • Null
        • ""
        • False
        • Undefined
        • NaN

Then the value of the object is false. Otherwise, its value is true even when the argument is the string "false"!

1.2 Boolean Object Properties

1.2.1 Constructor Properties

Grammar:

Boolean.constructor

Definition and Usage:

The constructor property returns a function prototype that creates a Boolean object (that is, a reference to the Boolean function that created the object).

Related examples:

Returns a function created from the prototype of the MyVar object:

var myvar = new Boolean (1);

Myvar.constructor;

Result output:

function Boolean () {[native code]}

1.2.2 Boolean prototype constructor

Grammar:

Boolean.prototype.name=value;

Definition and Usage:

The Prototypen property gives you the ability to add properties and methods to an object.

When constructing a prototype, all Boolean objects add properties or methods by default.

Note: Boolean.prototype is not a reference to a Boolean value, but a Boolean () object is.

           Note: Prototype is a global property, which is for almost all JavaScript objects.

Related examples:

Boolean.prototype.mycolor=function ()

{

if (this.valueof () ==true)

{

This.color= "Green";

} else

{

This.color= "Red";

}

}

Create a Boolean object and add the MyColor method:

var a = new Boolean (1);

A.,ycolor ();

var B=a.color;

Result output:

Green

1.3 Boolean Object method

1.3.1 ToString () method

Grammar:

Boolean.tostring ()

Definition and Usage:

The ToString () method converts a logical value to a string and returns the result (true or false).

          Note: JavaScript automatically calls this method when a Boolean object needs to be converted to a string.

1.3.2 ValueOf () method

Grammar:

Boolean.valueof ()

Definition and Usage:

The ValueOf () method returns the original value (TRUE or false) of the Boolean object.

Related examples:

Returns the original value of the Boolean object:

var bool = new Boolean (0).

var myvar = bool.valueof ();

MyVar Output Results:

False

2. Number Object type

The number object is the wrapper object for the original value.

Number Creation method new number ().

Grammar:

var num = new number (value);

2.1 Number Object Properties

2.1.1 Constructor Properties

Grammar:

Number.constructor;

Definition and Usage:

The constructor property returns a reference to the Boolean function that created this object.

Related examples:

Returns a reference to the number function that created this object:

Num.constructor;

Output Result:

function number () {[native code]}

2.1.2 Max_value Properties

Grammar:

Number.MAX_VALUE;

Definition and usage

The Max_value property is the largest number that can be represented in JavaScript.

It has an approximate value of 1.7976931348623157 x 10308.

           Note: numbers larger than max_value represent infinity.

Related examples:

Example 1

Returns the maximum number of javascript:

Number.MAX_VALUE;

return value:

1.7976931348623157e+308

Because Max_value is a property of the number object, you can use the Number.MAX_VALUE call.

You cannot get the Xnumber object (x.max_value) you created using your own.

Example 2

Max_value is a static property of the JavaScript number object and can only be called through Number.MAX_VALUE.

Using a custom Numberx (X.max_value) will not get the Max_value property;

var x = 100;

X.max_value;

X Output results:

Undefined

3. String Object type

JavaScript objects-An explanation of the basic packaging type

Related Article

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.