JavaScript Wrapper object Usage Introduction _javascript Tips

Source: Internet
Author: User
Tags access properties wrapper

JavaScript is an object-oriented language, using the "." Operators can access properties and methods of objects, while for basic types (null, undefined, bool, number, string) should be value types, without attributes and methods, however

Copy Code code as follows:

var s= ' This is a string ';
alert (s.length);
Alert (S.indexof (' is '));

The result is simple, but it's strange to think about it, string is not a value type! How has the attribute and has the method!

Built-in objects

JavaScript has a series of built-in objects to create the basic features of the language, specifically look at

Boolean

A Boolean object represents two values: "True" or "false." When invoked as a constructor (with operator new), the Boolean () converts its arguments to a Boolean value and returns a Boolean object that contains the value. If invoked as a function (without operator new), Boolean () converts only its arguments to an original Boolean value, and returns the value if the value argument is omitted or set to 0,-0, NULL, "", false, undefined, or NaN, The object is set to False. Otherwise set to true (even if the value parameter is the string "false").

Boolean objects include the ToString and ValueOf methods, and Boolean is most commonly used for simple judgments of true or false values in conditional statements, and a combination of Boolean and conditional statements provides a way to create logic using JavaScript.

Number

The number object is a numeric wrapper that contains several read-only properties

Maximum number of max_value:1.7976931348623157e+308//javascript can handle
Minimum number that min_value:5e-324//javascript can handle
negative_infinity:-infiny//Negative infinity
positive_infinity:infinity//Positive Infinity
Nan:nan//non-digital
Number objects also have methods that you can use to format or convert numeric values

toexponential//string representation of numbers returned as exponential
tofixed//rounding number to a specified decimal number
toprecision//Converts a value of an object to exponential count when it exceeds a specified number of digits
ToString//Returns a string representation of a number
valueof//Inherits from Object
String

The String object is the wrapper for the literal value. In addition to storing text, a string object contains a property and various methods to manipulate or collect information about the text, and the string object does not need to be instantiated to be able to use it.

The string object has only one read-only length property to return the length of the string. A string object has many methods

Charat
charCodeAt
concat
fromCharCode
indexof
LastIndexOf
match
Replace
Search
Slice
Split
substr
substring
toLowerCase
toUpperCase

Wrapping Objects

In addition to the above three objects, JavaScript also has the date, Array, math and other built-in objects, these three are often used to display, so very familiar with the built-in objects can see what the above example is going on.

As long as it refers to the properties and methods of the string, JavaScript converts the string value to a built-in object string through the new string, which is destroyed once the reference is finished. So the code above is actually using the length property of the string object and the IndexOf method.

Similarly, the processing of numbers and Boolean values is similar. , null and undefined have no corresponding object. Since there are objects generated, can this be

Copy Code code as follows:

var s= ' This is a string ';
s.len=10;
alert (S.len);

The results did not return 10, but undefined! It's not a good deal, it's an object! As mentioned just now, the second line of code creates a temporary string object and then destroys it, and the third line creates a new temporary object (which is one of the reasons why the low version of IE frequently handles string inefficiencies) and naturally does not have the Len attribute. This created temporary object becomes the wrapper object. It never occurred to me that a simple line of code would contain so many poses.

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.