使用 Javascript parseInt() 和一个基数参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10398834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:44:25  来源:igfitidea点击:

Using Javascript parseInt() and a Radix Parameter

javascriptparseintradix

提问by shrewdbeans

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

谁能解释一下parseInt()函数的工作原理以及基数参数是什么?

As a case study, I am trying to get to grips with this code snippet:

作为一个案例研究,我试图掌握这个代码片段:

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

Can you also explain how this code works? Why is formField.attr('maxlength')there twice? I find the use of operators here pretty darn confusing!

你能解释一下这段代码是如何工作的吗?为什么formField.attr('maxlength')有两次?我发现这里使用运算符非常令人困惑!

How does the Radix Parameter work in this example?

在这个例子中基数参数是如何工作的?

回答by Alnitak

The radixis 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.

radix是 的另一个名称base,即 2 表示二进制,10 表示十进制,16 表示十六进制,在Mozilla 开发者网络站点上有更详细的解释。

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).

在您的示例中,没有 radix parameter,因此解释器将回退到默认行为,该行为通常将数字视为十进制,除非它们以零(八进制)或0x(十六进制)开头。

回答by Peter Machowski

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)

在 ECMA 脚本 5 中,当字符串以 0 开头且未指定基数时,默认行为是十进制(与早期版本中的八进制相反)

Source: parseInt() on Mozilla Developer Network

来源:Mozilla 开发者网络上的 parseInt()

回答by Blake Basas

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

parseInt两个参数,第二个是可选的。字符串和基数。

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

字符串是要解析的值。如果提供的值不是字符串,它会将其转换为字符串。

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

基数2 到 36之间的整数,表示上述字符串的基数(数学数字系统中的基数)。

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

在您的代码片段中,未指定基数并假定为默认值 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.

您正在定义一个名为“maxChars”的变量。此变量等于对简写 IF 语句的评估。

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.

您正在从变量中获取属性,该属性应该是一个名为“maxLength”的选择器“formField”。该值将作为整数返回,它将回退到它的默认基数。

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".

IF 语句检查返回值是真还是假。0、false、ectcetera 将导致变量“maxChars”的值设置为“counters”组合文本。如果为 true,它将导致将变量设置为名为“maxLength”的选择器“formField”属性。

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.

是否有两次,因为一个用于 IF 语句评估,另一个用作 IF 语句中的条件结果为 TRUE 时的值。