Home > Web Front-end > JS Tutorial > Introduction to using JavaScript wrapper objects_javascript skills

Introduction to using JavaScript wrapper objects_javascript skills

WBOY
Release: 2016-05-16 17:24:07
Original
919 people have browsed it

JavaScript is an object-oriented language. You can use the "." operator to access the properties and methods of an object. Basic types (null, undefined, bool, number, string) should be value types without properties and methods. However,

Copy code The code is as follows:

var s='this is a string';
alert(s.length);
alert(s.indexOf('is'));

The result is very simple, but if you think about it carefully, it’s really strange. String is not a value type! How come it has attributes and methods!

Built-in Objects

JavaScript has a series of built-in objects to create the basic functions of the language, take a look at it in detail

Boolean

Boolean objects represent two values: "true" or "false". When called as a constructor (with operator new), Boolean() will convert its argument to a Boolean value and return a Boolean object containing that value. If called as a function (without operator new), Boolean() will simply convert its argument to a primitive Boolean value and return this 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 argument is the string "false").

Boolean objects include toString and valueOf methods. Boolean is most often 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

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

•MAX_VALUE:1.7976931348623157e 308 //The maximum number that JavaScript can handle
•MIN_VALUE:5e-324 //The smallest number that JavaScript can handle
•NEGATIVE_INFINITY:-Infiny //Negative infinity
• POSITIVE_INFINITY:Infinity //Positive infinity
•NaN:NaN //Non-number
The Number object also has some methods that can be used to format or convert values

•toExponential //Return the string representation of the number in exponential form
•toFixed //Round Number to a number with specified decimal places
•toPrecision //When the value of the object exceeds the specified number of digits, It is converted to exponential notation
•toString //Returns the string representation of the number
•valueOf //Inherited from object
String

String objects are wrappers for text values. In addition to storing text, the String object contains a property and various methods to manipulate or collect information about the text. The String object does not need to be instantiated in order to be used.

String object has only a read-only length property used to return the length of the string. String objects have 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 are often used, so they are very familiar. Once you know the built-in objects, you can see what the above example is about.

As long as the properties and methods of a string are referenced, JavaScript will convert the string value into the built-in object String through new String(s). Once the reference ends, the object will be destroyed. So the above code is actually using the length property and indexOf method of the String object.

For the same reason, numbers and Boolean values ​​are processed similarly. , null and undefined have no corresponding objects. Since there is an object generated, can it be done like this

Copy code The code is as follows:

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

The result did not return 10, but undefined! Didn’t we agree that we are a match? As mentioned just now, 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 low versions of IE frequently handle strings inefficiently). Naturally There is no len attribute. The temporary object created becomes the packaging object. I didn't expect that a simple line of code could contain so many gestures.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template