jquery type Conversion

Source: Internet
Author: User

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

Convert to Digital

ECMAScript provides two ways to convert non-numeric primitive values into numbers, namely parseint () and parsefloat ().
Note: These methods are called only for string types (except number) to be run correctly for other types that return Nan.

For example:

JS Code
    1. var iNum1 = parseint ("1234blue");//returns 1234
    2. var iNum2 = parseint ("OxA"); Returns 10
    3. var iNum3 = parseint ("22.5″"); Returns 22
    4. var iNum4 = parseint ("Blue"); Returns NaN

The following other types of conversions

One, type conversion

1. Convert into a string

The interesting thing about the original values of ECMAScript's Boolean values, numbers, and strings is that they are pseudo-objects, which means that they actually have properties and methods.
Such as:

JS Code
varScolor = "Blue";
    1. alert (scolor.length);//outputs "4″

In summary, there are 3 primary primitive values, Boolean values, numbers, and strings that 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 simply 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 number type is special, it has two modes, the default mode and the base mode, the default mode, and the ToString () method simply outputs the numeric value with the corresponding string (whether it is an integer, a floating-point number, or a scientific notation).

JS Code
    1. var iNum1 = 10;
    2. var fNum2 = 10.0;
    3. Alert (inum1.tostring ()); Outputs "10″
    4. Alert (fnum2.tostring ()); Outputs "10″

The base mode of the ToString () method with the number type can be used to output numbers in different bases (radix).

JS Code
    1. var iNum = 10;
    2. Alert (inum.tostring (2)); Outputs "1010″
    3. Alert (inum.tostring (8)); Outputs "12″
    4. Alert (inum.tostring (16)); Outputs "A"

2. Convert to Digital

ECMAScript provides two ways to convert non-numeric primitive values into numbers, namely parseint () and parsefloat ().
Note: These methods are called only for string types (except number) to be run correctly for other types that return Nan.

For example:

JS Code
    1. var iNum1 = parseint ("1234blue");//returns 1234
    2. var iNum2 = parseint ("OxA"); Returns 10
    3. var iNum3 = parseint ("22.5″"); Returns 22
    4. var iNum4 = parseint ("Blue"); Returns NaN

The parseint () method also has a base mode that converts binary, octal, hexadecimal, or any other binary string into a decimal integer. The second parameter specifies which type of binary to parse.

JS Code
    1. var iNum1 = parseint ("AF");//returns 175
    2. var iNum2 = parseint ("10″,2"); Returns 2
    3. var iNum3 = parseint ("10″,8"); Returns 8
    4. var iNum4 = parseint ("10″,10"); Returns 10

Note: If the decimal number contains a leading 0, it is best to use cardinality 10, otherwise you will get the octal value.

JS Code
    1. var iNum1 = parseint ("010″"); Returns 8
    2. var iNum2 = parseint ("010″,8"); Returns 8
    3. var iNum3 = parseint ("010″,10");//returns 10

The Parsefloat () method is similar to the parseint () method, viewing each character starting at position 0 until the first non-valid character is found, and then converting the string before the character to a number. For this method, the first decimal point that appears is a valid character. If you use two decimal points, the second decimal point will be considered invalid. Another difference in using this method is that the string must represent a floating-point number in decimal form.

JS Code
    1. var fNum1 = parsefloat ("1234blue"); Returns 1234.0
    2. var fNum2 = parsefloat ("0xA"); Returns NaN
    3. var fNum3 = parsefloat ("22.5″"); Returns 22.5
    4. var fNum4 = parsefloat ("22.34.5″");//returns 22.34
    5. var fNum5 = parsefloat ("0908″);//returns NaN
    6. var fNum6 = parsefloat ("blue");//returns NaN

3. Forcing type conversions
The 3 types of coercion conversions available in ECMAScript are as follows:
(1). Boolean (value)
Converts the given value to a Boolean type.
The Boolean () function returns True when the value to be converted is a string of at least one character, not a 0 number, or an object. If the value is an empty string, the number 0, undefined, or null, it returns FALSE.
Such as:

JS Code
    1. var b1 = Boolean (""); False
    2. var b2 = Boolean ("Hi");//true
    3. var b3 = Boolean (+);//true
    4. var b4 = Boolean (null );//false
    5. var b5 = Boolean (0);//false
    6. var b6 = Boolean (new Object ());//true

(2). Number (value)
Converts the given value to a number (which can be an integer or a floating point).
Remember that the parseint () and parsefloat () methods only convert the string before the first invalid character, so "4.5.6″ will be converted to" 4.5″. Coercion type conversion with number (), "4.5.6″ will return Nan because the entire string value cannot be converted to numbers." If the string can be fully converted, number () will determine whether to call the parseint () method or call the Parsefloat () method.
Such as:

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 (new Object ());//nan
    9. Number (100);//100

(3). String (value)
Converts the given value into a string.
The only difference from calling the ToString () method is that coercion of type conversions on null or undefined values can generate strings without throwing an error:

JS Code
    1. var s1 = String (null );//"NULL"
    2. var onull = null ;
    3. var s2 = onull.tostring ();//causes an error

Ii. type of reference
A reference type is often called a class, that is, 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 by this class, and all properties and methods in the object class appear in other classes (overwritten).

Properties of the object class:

(1). constructor--a reference (pointer) to the function that created the object. For the object class, the pointer points to the original object () function.

(2). prototype--a reference to the object's prototype of the object. For all classes, it returns an instance of Object objects by default. one, type conversion

1. Convert into a string

The interesting thing about the original values of ECMAScript's Boolean values, numbers, and strings is that they are pseudo-objects, which means that they actually have properties and methods.
Such as:

Js Code
var scolor = "Blue";
    1. alert (scolor.length); //outputs "4″

In summary, there are 3 primary primitive values, Boolean values, numbers, and strings that 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 simply outputs "true" or "false", and the result is determined by the value of the variable:

Js Code
var bfound = false ;
    1. Alert (bfound.tostring ()); //outputs "false"

The ToString () method of number type is special, it has two modes, the default mode and the base mode, the default mode, and the ToString () method simply outputs the numeric value with the corresponding string (whether it is an integer, a floating-point number, or a scientific notation).

Js Code
    1. var iNum1 = 10;
    2. var fNum2 = 10.0;
    3. Alert (inum1.tostring ()); //outputs "10″
    4. Alert (fnum2.tostring ()); //outputs "10″

The base mode of the ToString () method with the number type can be used to output numbers in different bases (radix).

Js Code
    1. var iNum = 10;
    2. Alert (inum.tostring (2)); //outputs "1010″
    3. Alert (inum.tostring (8)); //outputs "12″
    4. Alert (inum.tostring (16)); //outputs "A"

The parseint () method also has a base mode that converts binary, octal, hexadecimal, or any other binary string into a decimal integer. The second parameter specifies which type of binary to parse.

Js Code
    1. var iNum1 = parseint ( "AF", 16); //returns 175
    2. var iNum2 = parseint ( "10″,2"); //returns 2
    3. var iNum3 = parseint ( "10″,8"); //returns 8
    4. var iNum4 = parseint ( "10″,10"); //returns Ten

Note: If the decimal number contains a leading 0, it is best to use cardinality 10, otherwise you will get the octal value.

Js Code
    1. var iNum1 = parseint ( "010″"); //returns 8
    2. var iNum2 = parseint ( "010″,8"); //returns 8
    3. var iNum3 = parseint ( "010″,10"); //returns Ten

The Parsefloat () method is similar to the parseint () method, viewing each character starting at position 0 until the first non-valid character is found, and then converting the string before the character to a number. For this method, the first decimal point that appears is a valid character. If you use two decimal points, the second decimal point will be considered invalid. Another difference in using this method is that the string must represent a floating-point number in decimal form.

Js Code
  1. var fNum1 = parsefloat ( "1234blue"); //returns 1234.0
  2. var fNum2 = parsefloat ( "0xA"); //returns NaN
  3. var fNum3 = parsefloat ( "22.5″"); //returns 22.5
  4. var fNum4 = parsefloat ( "22.34.5″"); //returns 22.34
  5. var fNum5 = parsefloat ( "0908″"); //returns NaN
  6. var fNum6 = parsefloat ( "Blue"); //returns NaN

3. Forcing type conversions
The 3 types of coercion conversions available in ECMAScript are as follows:
(1). Boolean (value)
Converts the given value to a Boolean type.
The Boolean () function returns True when the value to be converted is a string of at least one character, not a 0 number, or an object. If the value is an empty string, the number 0, undefined, or null, it returns FALSE.
Such as:

Js Code
  1. var b1 = Boolean ( "");   //false;
  2. var b2 = Boolean ( "HI"); //true
  3. var b3 = Boolean (100); //true
  4. var b4 = Boolean ( null ); //false
  5. var b5 = Boolean (0); //false
  6. var b6 = Boolean ( new Object ()); //true

(2). Number (value)
Converts the given value to a number (which can be an integer or a floating point).
Remember that the parseint () and parsefloat () methods only convert the string before the first invalid character, so "4.5.6″ will be converted to" 4.5″. Coercion type conversion with number (), "4.5.6″ will return Nan because the entire string value cannot be converted to numbers." If the string can be fully converted, number () will determine whether to call the parseint () method or call the Parsefloat () method.
Such as:

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 ( new Object ()); //nan
  9. Number (100); //100

(3). String (value)
Converts the given value into a string.
The only difference from calling the ToString () method is that coercion of type conversions on null or undefined values can generate strings without throwing an error:

Js Code
    1. var s1 = String ( null ); //"NULL"
    2. var onull = null ;
    3. var s2 = onull.tostring (); //causes An error

Ii. type of reference
A reference type is often called a class, that is, 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 by this class, and all properties and methods in the object class appear in other classes (overwritten).

Properties of the object class:

(1). constructor--a reference (pointer) to the function that created the object. For the object class, the pointer points to the original object () function.

(2). prototype--a reference to the object's prototype of the object. For all classes, it returns an instance of Object objects 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.