Jquery type conversion

Source: Internet
Author: User

From: http://blog.csdn.net/kfanning/archive/2010/04/14/5485412.aspx

Convert to numbers

Ecmascript provides two methods to convert non-numeric values into numbers: parseint () and parsefloat ().
Note: Only when these methods are called for the string type (except number) can Nan be returned for other types be correctly run.

For example:

JS Code
  1. VaRInum1 = parseint ("1234blue"); // returns 1234
  2. VaRInum2 = parseint ("OXA"); // returns 10
  3. VaRInum3 = parseint ("22.5"); // returns 22
  4. VaRInum4 = parseint ("blue"); // returns Nan

Other types of conversion

I. type conversion

1. Convert to string

Ecmascript's boolean values, numbers, and string values are interesting because they are pseudo objects, which means they actually have attributes and methods.
For example:

JS Code
VaRScolor = "Blue ";
  1. Alert (scolor. Length); // outputs "4 ″

All in all, the three main primitive values, Boolean values, numbers, and strings all have the tostring () method. All objects defined by ecmascript have the tostring () method, whether it is a pseudo object or a real object.

The tostring () method of the boolean type only outputs "true" or "false", and the result is determined by the value of the variable:

JS Code
VaRBfound = False;
  1. Alert (bfound. tostring (); // outputs "false"

The tostring () method of the number type is special. It has two modes: default mode and base mode. The default mode and tostring () mode are used () the method outputs the numeric value (whether it is an integer, floating point number, or scientific Notation) with the corresponding string ).

JS Code
  1. VaRInum1 = 10;
  2. VaRFnum2 = 10.0;
  3. Alert (inum1.tostring (); // outputs "10 ″
  4. Alert (fnum2.tostring (); // outputs "10 ″

The base mode of the tostring () method of the number type can be used to output numbers with different base (base) numbers.

JS Code
  1. VaRInum = 10;
  2. Alert (inum. tostring (2); // outputs "1010 ″
  3. Alert (inum. tostring (8); // outputs "12 ″
  4. Alert (inum. tostring (16); // outputs ""

2. convert to a number

Ecmascript provides two methods to convert non-numeric values into numbers: parseint () and parsefloat ().
Note: Only when these methods are called for the string type (except number) can Nan be returned for other types be correctly run.

For example:

JS Code
  1. VaRInum1 = parseint ("1234blue"); // returns 1234
  2. VaRInum2 = parseint ("OXA"); // returns 10
  3. VaRInum3 = parseint ("22.5"); // returns 22
  4. VaRInum4 = parseint ("blue"); // returns Nan

The parseint () method also has the base mode, which can convert binary, octal, hexadecimal, or any other hexadecimal string to a decimal integer. The second parameter specifies which method to parse.

JS Code
  1. VaRInum1 = parseint ("AF", 16); // returns 175
  2. VaRInum2 = parseint ("10", 2); // returns 2
  3. VaRInum3 = parseint ("10", 8); // returns 8
  4. VaRInum4 = parseint ("10", 10); // returns 10

Note: If the decimal number contains leading 0, it is best to use base 10; otherwise, the octal value is obtained.

JS Code
  1. VaRInum1 = parseint ("010"); // returns 8
  2. VaRInum2 = parseint ("010", 8); // returns 8
  3. VaRInum3 = parseint ("010", 10); // returns 10

The parsefloat () method is similar to the parseint () method. Each character is viewed from position 0 until the first invalid character is found, then, convert the string before the character to a number. For this method, the first decimal point is a valid character. If two decimal points are used, the second decimal point is considered invalid. Another difference with this method is that the string must represent a floating point number in decimal format.

JS Code
  1. VaRFnum1 = parsefloat ("1234blue"); // returns 1234.0
  2. VaRFnum2 = parsefloat ("0xa"); // returns Nan
  3. VaRFnum3 = parsefloat ("22.5"); // returns 22.5
  4. VaRFnum4 = parsefloat ("22.34.5"); // returns 22.34
  5. VaRFnum5 = parsefloat ("0908"); // returns Nan
  6. VaRFnum6 = parsefloat ("blue"); // returns Nan

3. Forced type conversion
The three types of mandatory conversions available in ecmascript are as follows:
(1). boolean (value)
Converts a given value to a Boolean value.
If the value to be converted is a string of at least one character, a non-zero number, or an object, the Boolean () function returns true. If the value is a Null String, number 0, undefined, or null, it returns false.
For example:

JS Code
  1. VaRB1 = Boolean (""); // false;
  2. VaRB2 = Boolean ("hi"); // true
  3. VaRB3 = Boolean (100); // true
  4. VaRB4 = Boolean (Null); // False
  5. VaRB5 = Boolean (0); // false
  6. VaRB6 = Boolean (NewObject (); // true

(2). Number (value)
Converts a given value to a number (which can be an integer or a floating point number ).
Remember that the parseint () and parsefloat () methods only convert strings before the first invalid character, so "4.5.6" will be converted to "4.5 ″. Use number () for forced type conversion, "4.5.6" will return Nan, because the entire string value cannot be converted to a number. If the string can be fully converted, number () determines whether to call the parseint () method or the parsefloat () method.
For example:

JS Code
  1. Number (False); // 0
  2. Number (True); // 1
  3. Number (undefined); // Nan
  4. Number (Null); // 0
  5. Number (5.5); // 5.5
  6. Number ("56"); // 56
  7. Number ("5.6.7"); // Nan
  8. Number (NewObject (); // Nan
  9. Number (100); // 100

(3). String (value)
Converts a given value to a string.
The only difference from calling the tostring () method is that forced type conversion of null or undefined values can generate a string without causing an error:

JS Code
  1. VaRS1 = string (Null); // "Null"
  2. VaROnull =Null;
  3. VaRS2 = onull. tostring (); // causes an error

Ii. Reference Type
A reference type is usually called a class. That is to say, when a reference value is encountered, the object is processed. Ecmascript defines "object definitions", which are logically equivalent to classes in other programming languages.

1. Object Class
All classes in ecmascript are inherited from this class, And all attributes and methods in the object class will appear in other classes (overwritten ).

Attributes of the object class:

(1). constructor -- reference to the function of the created object (pointer ). For object classes, this pointer points to the original object () function.

(2). prototype -- reference to the object prototype of the object. For all classes, it returns an instance of the object by default. I. type conversion

1. Convert to string

Ecmascript's boolean values, numbers, and string values are interesting because they are pseudo objects, which means they actually have attributes and methods.
For example:

JS Code
VaRScolor = "Blue ";
  1. Alert (scolor. Length); // outputs "4 ″

All in all, the three main primitive values, Boolean values, numbers, and strings all have the tostring () method. All objects defined by ecmascript have the tostring () method, whether it is a pseudo object or a real object.

The tostring () method of the boolean type only outputs "true" or "false", and the result is determined by the value of the variable:

JS Code
VaRBfound = False;
  1. Alert (bfound. tostring (); // outputs "false"

The tostring () method of the number type is special. It has two modes: default mode and base mode. The default mode and tostring () mode are used () the method outputs the numeric value (whether it is an integer, floating point number, or scientific Notation) with the corresponding string ).

JS Code
  1. VaRInum1 = 10;
  2. VaRFnum2 = 10.0;
  3. Alert (inum1.tostring (); // outputs "10 ″
  4. Alert (fnum2.tostring (); // outputs "10 ″

The base mode of the tostring () method of the number type can be used to output numbers with different base (base) numbers.

JS Code
  1. VaRInum = 10;
  2. Alert (inum. tostring (2); // outputs "1010 ″
  3. Alert (inum. tostring (8); // outputs "12 ″
  4. Alert (inum. tostring (16); // outputs ""

The parseint () method also has the base mode, which can convert binary, octal, hexadecimal, or any other hexadecimal string to a decimal integer. The second parameter specifies which method to parse.

JS Code
  1. VaRInum1 = parseint ("AF", 16); // returns 175
  2. VaRInum2 = parseint ("10", 2); // returns 2
  3. VaRInum3 = parseint ("10", 8); // returns 8
  4. VaRInum4 = parseint ("10", 10); // returns 10

Note: If the decimal number contains leading 0, it is best to use base 10; otherwise, the octal value is obtained.

JS Code
  1. VaRInum1 = parseint ("010"); // returns 8
  2. VaRInum2 = parseint ("010", 8); // returns 8
  3. VaRInum3 = parseint ("010", 10); // returns 10

The parsefloat () method is similar to the parseint () method. Each character is viewed from position 0 until the first invalid character is found, then, convert the string before the character to a number. For this method, the first decimal point is a valid character. If two decimal points are used, the second decimal point is considered invalid. Another difference with this method is that the string must represent a floating point number in decimal format.

JS Code
  1. VaRFnum1 = parsefloat ("1234blue"); // returns 1234.0
  2. VaRFnum2 = parsefloat ("0xa"); // returns Nan
  3. VaRFnum3 = parsefloat ("22.5"); // returns 22.5
  4. VaRFnum4 = parsefloat ("22.34.5"); // returns 22.34
  5. VaRFnum5 = parsefloat ("0908"); // returns Nan
  6. VaRFnum6 = parsefloat ("blue"); // returns Nan

3. Forced type conversion
The three types of mandatory conversions available in ecmascript are as follows:
(1). boolean (value)
Converts a given value to a Boolean value.
If the value to be converted is a string of at least one character, a non-zero number, or an object, the Boolean () function returns true. If the value is a Null String, number 0, undefined, or null, it returns false.
For example:

JS Code
  1. VaRB1 = Boolean (""); // false;
  2. VaRB2 = Boolean ("hi"); // true
  3. VaRB3 = Boolean (100); // true
  4. VaRB4 = Boolean (Null); // False
  5. VaRB5 = Boolean (0); // false
  6. VaRB6 = Boolean (NewObject (); // true

(2). Number (value)
Converts a given value to a number (which can be an integer or a floating point number ).
Remember that the parseint () and parsefloat () methods only convert strings before the first invalid character, so "4.5.6" will be converted to "4.5 ″. Use number () for forced type conversion, "4.5.6" will return Nan, because the entire string value cannot be converted to a number. If the string can be fully converted, number () determines whether to call the parseint () method or the parsefloat () method.
For example:

JS Code
  1. Number (False); // 0
  2. Number (True); // 1
  3. Number (undefined); // Nan
  4. Number (Null); // 0
  5. Number (5.5); // 5.5
  6. Number ("56"); // 56
  7. Number ("5.6.7"); // Nan
  8. Number (NewObject (); // Nan
  9. Number (100); // 100

(3). String (value)
Converts a given value to a string.
The only difference from calling the tostring () method is that forced type conversion of null or undefined values can generate a string without causing an error:

JS Code
  1. VaRS1 = string (Null); // "Null"
  2. VaROnull =Null;
  3. VaRS2 = onull. tostring (); // causes an error

Ii. Reference Type
A reference type is usually called a class. That is to say, when a reference value is encountered, the object is processed. Ecmascript defines "object definitions", which are logically equivalent to classes in other programming languages.

1. Object Class
All classes in ecmascript are inherited from this class, And all attributes and methods in the object class will appear in other classes (overwritten ).

Attributes of the object class:

(1). constructor -- reference to the function of the created object (pointer ). For object classes, this pointer points to the original object () function.

(2). prototype -- reference to the object prototype of the object. For all classes, it returns an instance of the object by default.

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.