Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

javascript - parseInt alternative

Firstly - my description ;)

I've got a XmlHttpRequests JSON response from the server. MySQL driver outputs all data as string and PHP returns it as it is, so any integer is returned as string, therefore:

Is there any fast alternative (hack) for parseInt() function in JS which can parse pure numeric string, e.g.

var foo = {"bar": "123"};
...
foo.bar = parseInt(foo.bar); // (int) 123
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To convert to an integer simply use the unary + operator, it should be the fastest way:

var int = +string;

Conversions to other types can be done in a similar manner:

var string = otherType + "";
var bool = !!anything;

More info.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...