传递一个整数作为 `toString` 的参数有什么作用? [英] What does passing an integer number as the argument for `toString` do?

查看:69
本文介绍了传递一个整数作为 `toString` 的参数有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 JavaScript 课程,但没有关于 toString 方法的指南,JavaScript 中这两个输出的目的是什么:

I'm taking a course on JavaScript but have no guide on toString method, what's the purpose of these two outputs in JavaScript:

  1. (35).toString(36) >>> "z"!!!

(35).toString(37) >>> 抛出一个 RangeError!!!

(35).toString(37) >>> throws a RangeError!!!

我对为什么会得到这些结果感到非常困惑,如果有人能对此有所了解,我将不胜感激.

I am utterly confused as to why I am getting these results, I would really appreciate it if someone could shed some light on this.

推荐答案

tldr: Number.prototype.toString 可以采用一个名为 radix 的参数来指定用于相关数字的字符串表示的数字基数.<小时>Object.prototype.toString()

tldr: Number.prototype.toString can take an argument called radix that specifies the numeric base to use for the string representation of the number in question.


Object.prototype.toString()

一个对象的 toString 方法应该返回该对象的字符串表示(我说假设"是因为你可以修改它所以它不返回一个字符串).35 显然不是一个对象,它是一个原始类型,但是您像使用对象一样使用它,这会导致 JavaScript 为该 toString 创建一个临时的 Number 对象 调用(参见 this StackOverflow 上关于自动装箱的回答).

An object's toString method is supposed to return a string representation of that object (I say "supposed" because you can modify it so it doesn't return a string). 35 is clearly not an object, it is a primitive, but you are using it like an object, which causes JavaScript to create a temporary Number object for that toString call (see this StackOverflow answer on autoboxing).


Number.prototype.toString()

关于通过将 36 传递给 (35).toString 得到的令人困惑的行为:这是因为 Number.prototype.toString 可以接受一个参数,你可以用于指定用于该数字的字符串表示的数字基数,参数必须是整数(或任何其他可以强制为整数的值,例如 35..toString([20])35..toString([20])code>) 在 236 之间,所以 2 <= [radix] <= 36 (这意味着你的第二个例子会抛出一个RangeError).

About the confusing behavior you are getting by passing 36 to (35).toString: it is because Number.prototype.toString can take an argument that you can use to specify the numeric base to use for the string representation of that number, the argument must be an integer (or any other value that can be coerced to an integer, e.g 35..toString([20])) between 2 and 36, so 2 <= [radix] <= 36 (this means your second example will throw a RangeError).

因此,当您执行 (35).toString(36) 时,会发生两件事(不一定按照我的顺序,而且很可能在一个步骤中完成,35 ====> [基数"指定的数字格式中 35 的字符串表示]):

So, when you execute (35).toString(36), 2 things happen (not necessarily in my order, and most likely it is done in a single step, 35 ====> [string representation of 35 in numeric format specified by "radix"]):

  1. 生成数字 35 的字符串表示.
  2. 将步骤#1 中生成的字符串转换为radix 指定的数字基数.
  1. Generate a string representation of the number 35.
  2. Convert the string generated in step #1 to the number base specified by radix.

例如,如果您想要二进制形式的 35 字符串表示:

For example, if you wanted a string representation of 35, in binary form:

console.log(35..toString(2)); // "100011"

有趣的事实:语法 [integer]..[method] 在 JavaScript 中是完全有效的,第一个 . 被解释为小数点,后者作为 . 在对象的方法/属性的名称之前.

Fun fact: the syntax [integer]..[method] is totally valid in JavaScript, the first . is interpreted as a decimal point, the latter as the . that precedes the name of an object's method/property.


基数

如果你不知道什么是基数"(因为我之前不知道这个问题,不,我不是穴居人,英语不是我的母语),这是我得到的定义谷歌搜索基数含义":

If you don't know what a "radix" is (because I didn't prior to this question, and no, I am no caveman, English is not my native language), here is the definition I got by a Google search for "radix meaning":

计数系统的基础.

这篇关于传递一个整数作为 `toString` 的参数有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆