javascript - JSLint 认可的将数字转换为字符串的方法是什么?

标签 javascript jslint

我总是通过向数字添加空字符串来将数字转换为字符串:

var string = 1 + '';

但是,JSLint 提示此方法预期为“String”,而看到了“'''”。,而且它看起来确实有点难看。

有更好的方法吗?

最佳答案

我相信 JSLint 认可的方法是在号码上调用 .toString():

var stringified = 1..toString();
// Note the use of the double .. to ensure the the interpreter knows 
// that we are calling the toString method on a number -- 
// not courting a syntax error.
// You could also theoretically call 1["toString"];

关于javascript - JSLint 认可的将数字转换为字符串的方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5821950/

相关文章:

javascript - 剑道网格增删改查 : how make update

javascript - 阻止选择在 IE 中有效但在 Chrome 中无效的文本?

javascript - 如何将 id 传递给文件

javascript - 函数在定义之前被使用 - JSLint

javascript - 为什么 jslint 不能与 npm 包一起使用

javascript - 无法将文本分配给 Node 中的常量

javascript - 如何打开文件系统: from a link中的文件

javascript - JSLint 和我的 Javascript - 使该脚本符合要求的最佳实践

javascript -+++ 和 --- 在 JavaScript 中是什么意思?

javascript - 关于函数定义和调用顺序的最佳实践是什么?