Summary of creating objects in JS, Summary of creating objects in JS

Source: Internet
Author: User

Summary of creating objects in JS, Summary of creating objects in JS

In JS, we often use objects, including numbers, arrays, strings, dates, and so on. objects are nothing more than data of attributes and methods. Objects can be created using Constructors (new + common functions,

Var num = new Number (value );

Var num = Number (value); value indicates the value of the object.

Although sometimes we do not need to do this, we can directly call the corresponding function to achieve the goal, such as Number (), String (), Array (), Boolean (), Date () and so on.

You can do this directly:

Var num = Number (value)

In an object, the attribute is the attribute of the constructor, rather than the attribute of the object defined by the constructor.
For numeric objects, its attributes include:

Attribute Description
Constructor Returns a reference to the Number function that creates this object.
MAX_VALUE The maximum number that can be expressed.
MIN_VALUE The smallest number that can be expressed.
NaN Non-numeric value.
NEGATIVE_INFINITY Negative infinity. This value is returned when overflow occurs.
POSITIVE_INFINITY Positive infinity. This value is returned when overflow occurs.
Prototype Allows you to add attributes and methods to objects.

For example

Var num = Number (). MIN VALUE

But this cannot be used.

Var num = Number (1 );

Var mxnum = num. MAX VALUE

Similarly, a method is an object method, rather than a constructor method, that is, a method cannot be called by a constructor to execute an action. Instead, a method should be called through an object.

For numeric objects, the methods include:

Method Description
ToString Converts a number to a string and uses the specified base number.
ToLocaleString Converts a number to a string in the format of a local number.
ToFixed Converts a number to a string. The result is a number of the specified digits after the decimal point.
ToExponential Converts an object value to an exponential notation.
ToPrecision Format the number to the specified length.
ValueOf Returns the basic numeric value of A Number object.

For example

Var num = Number (123 );

Var OXnum = num. toString (16); // convert the value of the num object to a hexadecimal number.

Or

Var num = 123;

Var OXnum = num. toString (16); // in this case, JS converts num into a Number object and then calls its method.

This is also true for other types of objects and will not be repeated.



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.