function createRandomId() {
var i, random;
var uuid = '';
for (i = 0; i < 32; i++) {
random = Math.random() * 16 | 0;
if (i === 8 || i === 12 || i === 16 || i === 20) {
uuid += '-';
}
uuid += (i === 12 ? 4 : (i === 16 ? (random & 3 | 8) : random)).toString(16);
}
return uudi;
}
因此,在上面的函数中,我们创建了一个随机 ID 号。在 for 循环中,当我们将 id 添加到全局 uuid
变量时,我们使用接受一个参数的 toString()
方法。但是当我阅读 MDN doc(MDN)时,他们提到这个方法不应该接受参数。
所以在我的例子中,我很困惑为什么这个方法接受参数并使用它?
最佳答案
您正在查看错误的 MDN 页面。在代码中调用 toString
的值:
(i === 12 ? 4 : (i === 16 ? (random & 3 | 8) : random)).toString(16)
是一个数字,而不是一个对象。
Number.prototype.toString()确实接受一个参数:
Syntax
numObj.toString([radix])
Parameters
radix
Optional. An integer in the range 2 through 36 specifying the base to use for representing numeric values.
您的代码是否有一个对象 - 例如
uuid += { foo: random }.toString(16);
那么toString(16)
中的16确实毫无意义。
关于javascript - 就我而言,javascript 中的 toString() 方法接受 argument 。 MDN 上哪里有不同的信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51530921/