Using Javascript parseInt() and a Radix Parameter

后端 未结 3 1997
梦毁少年i
梦毁少年i 2020-12-06 16:30

Can anyone explain how the parseInt() functions works and what the Radix Parameter is?

As a case study, I am trying to get to grips

3条回答
  •  暖寄归人
    2020-12-06 17:05

    parseInt takes two parameters, the second one is optional. String and Radix.

    String is the value to parse. If the value provided is not a string it will convert it to a string.

    Radix is an integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the above mentioned string.

    In your code snippet the Radix isn't specified and is assumed to be default 16.

    var maxChars = parseInt( formField.attr('maxlength') ? formField.attr('maxlength') : counter.text() );
    

    You are defining a variable called "maxChars". This variable is equal to the evaluation of a short hand IF statement.

    You are getting the attribute from the variable which is expected to be a selector "formField" called "maxLength". The value will return as a integer, it will fallback on it's default radix.

    The IF statement checks if the returned value is true or false. 0, false, ectcetera would result in the value of the variable "maxChars" to be set to "counters" combined text. IF true it would result in the variable to be set as selector "formField" attribute called "maxLength".

    formField.attr('maxlength')
    

    Is there twice because one is used in an IF statement evaluation and the other is used as the value if the condition in the IF statement results as TRUE.

提交回复
热议问题