Introduction to JavaScript packaging objects _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces some knowledge points about packaging objects in JavaScript, including built-in objects. JavaScript is an object-oriented language and uses ". "operator can access the attributes and methods of the object, and the basic type (null, undefined, bool, number, string) should be value type, there is no attribute and method, however

The Code is as follows:


Var s = 'this is a string ';
Alert (s. length );
Alert (s. indexOf ('is '));

The result is very simple, but it is strange to think about it carefully. Isn't string a value type! Why are there attributes and methods available!

Built-in object

JavaScript has a series of built-in objects to create the basic functions of the language. For more information, see

Boolean

Boolean indicates two values: "true" or "false ". When called as a constructor (with the new operator), Boolean () converts its parameters into a Boolean value and returns a Boolean object containing the value. When called as a function (without the new operator), Boolean () will only convert its parameters into an original Boolean value and return this value. If the value parameter is omitted, or set to 0,-0, null, "", false, undefined, Or NaN, the object is set to false. Otherwise, set it to true (even if the value parameter is a string "false ").

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

Number

A Number object is a value wrapper that contains several read-only attributes.

• MAX_VALUE: 1.7976931348623157e + 308 // maximum number of JavaScript Processes
• MIN_VALUE: 5e-324 // minimum number of JavaScript Processes
• NEGATIVE_INFINITY:-Infiny // negative infinity
• POSITIVE_INFINITY: Infinity // positive Infinity
• NaN: NaN // non-numeric
There are still some methods for the Number object, which can be used to format or convert the value

• ToExponential // returns the string representation of the number in an exponential form
• ToFixed // rounds the Number to a Number of the specified decimal places
• ToPrecision // convert an object value into an exponential notation when it exceeds the specified number of digits
• ToString // return the string representation of a number
• ValueOf // inherits from object
String

The String object is the wrapper of the text value. In addition to text storage, the String object contains an attribute and various methods to operate or collect information about the text. The String object can be used without being instantiated.

The String object only has one read-only length attribute to return the length of the String. The String object has many methods.

• CharAt
• CharCodeAt
• Concat
• FromCharCode
• IndexOf
• LastIndexOf
• Match
• Replace
• Search
• Slice
• Split
• Substr
• Substring
• ToLowerCase
• ToUpperCase

Packaging object

In addition to the above three objects, JavaScript also has built-in objects such as Date, Array, and Math. These three objects are often displayed and used, so they are very familiar, once you know the built-in object, you can see what happened in the above example.

As long as the properties and methods of the String are referenced, JavaScript will convert the String value to the built-in object String through the new String (s) method. Once the reference ends, the object will be destroyed. Therefore, the above Code is actually using the length attribute and indexOf method of the String object.

In the same way, numbers and boolean values are processed similarly ., Null and undefined have no corresponding objects. Can this problem be solved if an object is generated?

The Code is as follows:


Var s = 'this is a string ';
S. len = 10;
Alert (s. len );

The result does not return 10, but is undefined! Isn't it a good object! As mentioned earlier, the second line of code only creates a temporary String object and then destroys it, the third line of code creates a new temporary object (this is one of the reasons why earlier IE versions frequently process low string efficiency). Naturally, there is no len attribute. The created temporary object becomes the packaging object. I didn't expect a line of simple code to include so many gestures.

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.