JSLint批准的将数字转换为字符串的方法是什么? [英] What is the JSLint approved way to convert a number to a string?

查看:85
本文介绍了JSLint批准的将数字转换为字符串的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've always converted numbers to strings by adding an empty string to them:

var string = 1 +'';

但是,JSLint使用预期的'String'来抱怨此方法,而是看到''''。,它确实看起来有点难看。

However, JSLint complains of this method with Expected 'String' and instead saw ''''., and it does look a little ugly.

还有更好的方法吗?

推荐答案

我认为JSLint批准的方式是在号码上拨打 .toString()

I believe that the JSLint approved way is to call .toString() on the number:

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"];

这篇关于JSLint批准的将数字转换为字符串的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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