Using Javascript parseInt() and a Radix Parameter

不想你离开。 提交于 2019-11-28 00:39:18

The radix is another name for base, i.e. 2 for binary, 10 for decimal, 16 for hexadecimal, explained in more detail on the Mozilla Developer Network site.

In your example there is no radix parameter, so the interpreter will fall back to the default behaviour, which typically treats numbers as decimal, unless they start with a zero (octal) or 0x (hexadecimal).

In the ECMA Script 5 when the string starts with 0 and no radix is specified the default behavior is decimal (as opposed to the earlier versions in which it was octal)

Source: parseInt() on Mozilla Developer Network

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!