JavaScript Daily jumping Pit Series (ii)

Source: Internet
Author: User

1. Number () converts part of a non-numeric type to 0

Cast to a numeric type function:

Parsefloat, parseint

Pros: Return nan to non-numeric types uniformly

Disadvantage: A part of a string that conforms to a numeric type is also recognized as a numeric value

Parsefloat ("1")//1
parsefloat ("1s")//1, not desired parsefloat (null)//nanparsefloat (undefined)//Nanparsefloat ("") //NaN        
Number

Advantage: The string must all conform to the numeric type to be converted successfully

Disadvantage: Converting part of a non-numeric type to 0

Number ("1")//1Number ("1s")//Nannumber (null)//0, not desired number (UNDEBUG)//  Nannumber ("")//0, not desired          

where number (NULL) and No. ("") will return 0, not Nan, resulting in various bugs.

A solution that determines whether a numeric type is

Use the $.isnumeric function provided by jquery to judge.

$.isnumeric ("1")//true$.isnumeric ("1s")//false$.isnumeric (null)//False$.isnumeric ( Undefined)//False$.isnumeric ("")//false          

All right, with jquery's $.isnumeric function source code:

function(obj) {    // parsefloat NaNs numeric-cast false positives (null|true|false| "")    /////// return!jquery.isarray (obj) && (obj-parsefloat (obj) + 1) >= 0;}, /c9> 

2, "". The Split (",") function returns an array of length 1

Before talking about this problem, we need to say some special values of the Split function.

A special value case for the split function of a string

1, the parameter does not pass, returned contains the original string object, the length is 1 array.

"". Split ()//[""] "Xxcanghai". Split ()//["Xxcanghai"]   

2. Parameters pass an empty string, returning an array that separates each character of the original string

Wakahara string is empty string returns an empty array

"". Split ("")//[] "Xxcanghai". Split ("")//["X", "X", "C", "a", "N", "G", "H", "a", "I"]   

3, the original string is an empty string, the argument is not empty, will return an array containing an empty string

"". Split (",")//[""], error, [] "". Split ("Xxcanghai")//["], error, should be []   

Visible, the original string length is 0, and the delimiter length is greater than 0 o'clock, which returns an array containing an empty string.

Because of this problem, a variety of bugs have been caused.

JavaScript Daily jumping Pit Series (ii)

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.