简体   繁体   English

$ .trim和trim之间有什么区别吗?

[英]Is there any difference between $.trim and trim?

I have this simple example below which uses trim. 我在下面有一个简单的例子,它使用了trim。 As per the title of the question, is there any difference between them?. 根据问题的标题,它们之间有什么区别吗? As you can see below they have the same output. 如下所示,它们具有相同的输出。 If the answer is "No", which one is much better to use? 如果答案是“否”,哪一个更好用?

Currently I use .trim() because it's my first time seeing $.trim() . 目前我使用.trim()因为这是我第一次看到$.trim()

 var SampleTrim = ' TRIM '; console.log(SampleTrim.trim()); console.log($.trim(SampleTrim)); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

String.prototype.trim is not available below IE9, otherwise they are the same. IE9下面没有String.prototype.trim ,否则它们是相同的。

The recommended String.prototype.trim polyfill on MDN is exactly the same as that used in jQuery source . MDN上推荐的String.prototype.trim polyfill与jQuery 中使用的完全相同。

In most cases, the native implementation would be faster to use. 在大多数情况下,本机实现使用起来会更快。 This JSPerf by Sumit Gulati suggests that too. Sumit Gulati的这个JSPerf也暗示了这一点。

Realistically, the difference in performance would be quite negligible and I wouldn't base my decision on performance. 实际上,性能上的差异可以忽略不计,我不会根据性能做出决定。 However, it would be better to use the native implementation so that there's no dependency on jQuery. 但是,最好使用本机实现,以便不依赖于jQuery。 The large websites have dropped support for IE8 and I think it's safe to follow suit (: 大型网站已经放弃了对IE8的支持,我认为跟风是安全的(:

jQuery's trim() function was available long before JavaScript's native trim() function was added, which is available in browsers above IE9 and the like. jQuery的trim()函数在添加JavaScript的native trim()函数之前就已经可用了,这在IE9之上的浏览器中是可用的。

Use the native trim function for future projects. 将原生修剪功能用于将来的项目。

As I know, SimpleTrim.trim() is a native way to trim a string that modern brower support. 据我所知,SimpleTrim.trim()是修剪现代浏览器支持的字符串的本地方式。

And $.trim(SimpleTrim) is another way that jQuery help user in the past to trim string, when browser does not support native trim. $ .trim(SimpleTrim)是浏览器不支持原生修剪时jQuery帮助用户修剪字符串的另一种方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM