parseInt函数替代 [英] parseInt alternative

查看:169
本文介绍了parseInt函数替代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先 - 我的描述;)

我已经得到了来自服务器的XmlHtt prequests JSON响应。 MySQL的驱动器输出的所有数据作为字符串和PHP返回它,因为它是如此的任意整数返回为字符串,因此:

有没有快速替代(黑客)的parseInt()函数功能的JS可以解析纯数字字符串,如

 变种富= {吧:123};
...
foo.bar = parseInt函数(foo.bar); //(INT)123
 

解决方案

要转换成整数只需使用一元+运算符,它应该是最快的方法:

  VAR INT = +串;
 

换算到其它类型的可以以类似的方式来完成:

  VAR字符串= OTHERTYPE +;
VAR BOOL = !!什么;
 

更多信息

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

解决方案

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.

这篇关于parseInt函数替代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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