COCOS2D-JS Introductory One ([IsNaN () and Isfinite (), conversions between characters and number)

Source: Internet
Author: User

IsNaN (): Used to check if the parameter is a non-numeric value. Tip: Non-digital OH. (Not a number)
document.write (IsNaN (0))//return False
document.write (IsNaN (5-2))//return False
document.write (IsNaN ("Hello"))//return True
The isfinite (number) function is used to check whether its arguments are infinite.
Returns true if number is finite. Returns False if number is NaN (non-numeric) or infinity;

2): Characters:

Examples of simple operations for strings:
var a = "Cssrain";
var B = A.charat (a.length-1);  Intercepts the last character from string A. Output: N
var c = a.substring (0, 2); Intercept 1th, 2 characters from String A. Output: CS
var d = a.indexof (' s '); Finds the position where the first s appears from string A. Output: 1

3): The conversion between numbers and characters:

Numeric to Character:
var number_to_string = number + ""; Method 1: Add an empty string.
var number_to_string =string (number); Method 2: Use the string () function.
var number_to_string =number. ToString (); Method 3: Use the ToString () function.
Note: the ToString () method defaults to a 10 binary conversion.
If you want to use the 8 binary conversion You can write this: number. ToString (8);

character to number:
Var string_to_ The number = string – 0 ; //method 1:  the string minus 0.
Var string_to_number = number (String)  ; //Method 2: Use the number  () function.
Var string_to_number = parseint (String)  ; //Method 3: Use the parseint  () function.
cannot   with string+0 ;  in Method 1 This results in string concatenation instead of type conversions. The number function in the
method 2  is converted, which is more rigorous.
For example:
var a =  "19cssrain86";
Var b = number (a);   //output Nan.
If we use Method 3.
Var c = parseint (a);  //output  19
You can see that parseint () automatically ignores non-numeric portions.
parseint () takes only the integer part, ignoring the fractional part. The
Parsefloat () will also take the decimal part. Like the
and ToString (), the parseint has a binary, which is 10 binary by default.
If you want to use 8 binary, you can write this:  parseint (  077  , 8 ); //  output 63 = 7 *  8 + 7
When the character starts with 0, we must specify the   second parameter  , or JS may be converted to 8 binary.  

4): Definition of function:
Method 1: General definition
function Square (x) {
return x*x;
}
Method 2: Function Direct Volume definition
var square = function (x) {return x*x;} Recommended Use
Method 3: Construct the parameters
var square = new Function ("X", "Return x*x;"); Low efficiency

COCOS2D-JS Introductory One ([IsNaN () and Isfinite (), conversions between characters and number)

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.