本文介绍了方法与基本JS?我应该使用toString吗?parseInt?jQuery的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从我找到toString()函数以来,这就是我一直在想的一个问题,但是从来没有想过要问.我应该使用基本的JS还是执行相同功能的函数?

This is a question I've been wondering about ever since I found the toString() function, but have never bothered to ask. Should I use basic JS or the function that does the same thing?

现在,请不要误会我,我意识到toString具有其可赎回的品质,就像将函数转换为字符串一样.

Now, don't get me wrong, I realize toString has its redeeming qualities, like converting a function to a string.

var message = function() {
// multi
// line
// string
}.toString();

但是请承认:我们主要使用toString将数字转换为字符串.我们不能只是这样做吗?

But admit it: we mainly use toString for converting numbers to strings. Couldn't we just do this instead?

var myNumber = 1234;
var message = ''+myNumber;

这不仅更短,而且根据JSPerf所述,toString方法要慢97%!(证明: http://jsperf.com/tostring-vs-basic-js )就像我说的,我知道toString很有用,但是当人们对Java变量类型提出疑问时,通常会出现toString().这就像基本的Javascript.每个浏览器都可以引用.

Not only is this shorter, but according to JSPerf the toString method is 97% slower! (Proof: http://jsperf.com/tostring-vs-basic-js ) And as I said, I know toString is useful, but when people raise question about types of Javascript variables, toString() usually comes up. And this is, like, basic Javascript. Every browser can do quotes.

parseInt也一样.现在,在我发现parseInt之前,我发现将字符串乘以1会将其转换为数字.那是因为您自然不能乘以字符串,从而迫使Javascript将其视为数字.

Same goes for parseInt. Now, before I discovered parseInt, I discovered that multiplying a string by one would convert it to a number. That's because you cannot multiply a string, naturally, forcing Javascript to treat it as a number.

var message = "4321";
var myNumber = message*1;

现在,有趣的是,这比parseInt慢,但幅度不大.我还注意到一个空字符串或一个没有数字的字符串将返回0,而parseInt将返回NaN,因为字符串中没有数字.再一次,我意识到parseInt更快,并且可以转换为不同的基数.但是,乘法更短,可以在任何浏览器中使用,请记住,parseInt将仅返回整数.那么,为什么总是将它作为问题的答案,询问如何转换为数字/NaN是什么?

Now, interestingly, this is slower than parseInt, but not by much. I also noticed that an empty string, or one without numbers, will return 0, whereas parseInt will return NaN because there are no numbers in the string. Once again, I realize parseInt is faster and can convert to different bases. However, multiplying is shorter, will work in any browser, and parseInt, remember, will only return integers. So why does it always come up as the answer to questions, asking how to convert to numbers/what NaN is?

现在,这可能会有点偏离主题,但我实际上想知道关于jQuery的类似问题.再说一次,jQuery是我从未真正了解过的用途.Javascript代码是干净的,jQuery本身就是一个JS文件,因此它无法执行Javascript无法执行的任何操作.它可以简化某些功能和内容,但是为什么不只将这些功能复制到页面上,而忽略不使用的其余功能呢?仅仅包含jQuery来完成一项简单的任务似乎有点过头了.动画在这里也不能原谅,因为使用原生Javascript也可以做到这一点.那么为什么要使用jQuery?

Now this might be going a little bit off topic, but I actually wonder a similar thing about jQuery. Once again, jQuery is something I've never really understood the use for. Javascript code is clean and jQuery is in and of itself a JS file, so it cannot do anything Javascript can't do. It may simplify certain functions and stuff, but why not just copy those functions to your page then and leave out the remaining functions you don't use? It seems overkill to include jQuery merely to complete one simple task. And animation isn't excused either here - because that too can be done with native Javascript. So why jQuery?

最终我要问的是,当有更好的方法时,为什么出于这些目的需要这些东西?还是更好的方法?通常来说,使用函数会更好吗?

Ultimately what I'm asking is, why do we need these things for these purposes when there are better methods? Or are they better methods? Is using functions a better practive just in general?

推荐答案

除非您每秒每秒对亿万个数字调用 .toString() ,否则您会发现这是应用程序分析的瓶颈,这根本不是一个因素.

Unless you're calling .toString() on hundreds of millions of numbers every second and you've found that this is a bottleneck in your application through profiling, this should not be a factor at all.

如您所见,只需将字符串和数字加在一起即可隐式完成此操作,因此我看不到使用''+ n 代替的任何好处n.toString().当您实际上没有将 n 与字符串连接时,后者更具可读性.

As you've seen, this can be done implicitly by just adding a string and a number together, so I fail to see any benefit of using '' + n in place of n.toString(). The latter is more readable when you're not actually concatenating n with a string.

您是说 parseInt 并非在所有浏览器中都有效吗?如果您想将某些内容解析为整数,请使用 parseInt .如果您想将某些内容解析为浮点数(JavaScript实际上也没有特殊类型,所有数字均为浮点数),请使用 parseFloat .

Are you saying that parseInt doesn't work in every browser? If you want to parse something as an integer, use parseInt. If you want to parse something as a float (JavaScript doesn't actually have a special type for either, all numbers are floats), use parseFloat.

更常见的模式是使用 +'123',其行为与 1 *'123'完全相同. parseInt 可以正确处理空字符串,但是由于任何原因都无法按预期方式验证字符串.一元加号在出现错误的情况下返回 NaN ,但会错误地处理空格和空字符串.这是JavaScript的缺点之一,因此,如果您使用的是基数10,则实际上在这两者之间没有具体选择.

The more common pattern is using +'123', which has the exact same behavior as 1 * '123'. parseInt handles empty strings properly, but for whatever reason does not validate strings as you'd expect. The unary plus returns NaN in case of an error, but treats whitespace and empty strings incorrectly. It's one of JavaScript's shortcomings, so there's really no concrete choice between the two if you're working in base 10.

因为规范中包含了这些函数,这些函数可以将字符串转换为数字,并使用二进制运算符将字符串转换为数字,这是副作用,而不是主要目的.另外,您还可以使用 parseInt 解析不同基数的整数,这对于强制类型是不可能的.

Because the spec included these functions to convert strings into numbers and converting strings into numbers using binary operators like you're doing is a side effect, not the primary purpose. Also you can parse integers in different bases using parseInt, which isn't possible with type coercion.

如果从CDN加载jQuery,则很有可能用户的浏览器已经下载并缓存了它,这使得下载时间和膨胀几乎不存在.如果您进行自定义构建",我敢打赌,这会使网站在首次加载时变慢.

If you load jQuery from a CDN, then there's a really good chance that a user's browser has already downloaded it and has it cached, making download times and bloat almost nonexistent. If you make a "custom build", I'd bet that it'll make the site slower on first load.

一切都可以.每次写东西都没有必要重新发明轮子.

So can everything. There's no point in reinventing the wheel every time you write something.

这篇关于方法与基本JS?我应该使用toString吗?parseInt?jQuery的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 23:13