What is the JSLint approved way to convert a number to a string?

Deadly 提交于 2019-12-04 09:55:13

问题


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

var string = 1 + '';

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

Is there a better way?


回答1:


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



回答2:


(Sorry, it possibly would've been better to say this as a comment above but I haven't yet earned the right to post comments, so...)

Remember that jslint is not just validating whether your JavaScript will actually run, it is trying to enforce coding style with the aim of helping you produce more readable and maintainable code.

So 1 + '' works, but isn't necessarily the most readable option for everybody while explicit casting options (see the other answers) should be readable for everybody. Of course if nobody else will ever see your code you need only worry about whether you will be able to understand it if you come back to it next month, or next year...

Don't forget that the following two statements don't produce the same result:

var s1 = 1 + 3 + ''; // gives '4'
var s2 = '' + 1 + 3; // gives '13'

I assume 1 + '' is just a simplification for discussion though, or why not just use '1' in the first place?




回答3:


You can use the .toString() method like so:

var num = 1;
var str = num.toString();



回答4:


There's also (at least in Chrome): String(1) without new.

var n = 1, s = String(n);



回答5:


I am going to say "bug" or "mis-feature"

Cole Consider that

var x = "foobar" + 1;

is "approved" jslint. In any case, it is 100% valid Javascript.

Happy coding.


For comment:

This is why I prefer to use the string literal as the first operand as this shows intent -- knowing a language is fundamental to using a language.

The only place the duck-typing is an issues (in this case) is with an expression of the form a + b, where neither is a string literal. In this case a (or b) may evaluate to a string where it was expected to evaluate to a number (this would trigger the string concatenation vs. the expected numeric addition). If any of the operands are a string literal the intent is well-defined/described.

This particular "issue", however, is not present in the posted code; nor would it be eliminated with the use of toString over a string literal.




回答6:


In my opinion, we should use String(number) instead of number + '' or number.toString(), because

  • number + '' will trigger the JSLint error

  • number.toString() will fail in case number is null or undefined, and you will have TypeError: number is undefined / null




回答7:


Recently, JSLint was in beta. The new version no longer complains about this code:

function convert(x) {
    'use strict';
    // alert(typeof x);
    x = x + "";
    // alert(typeof x);
    return x;
}


convert(3);


来源:https://stackoverflow.com/questions/5821950/what-is-the-jslint-approved-way-to-convert-a-number-to-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!