javascript – jquery.trim与string.trim相比

前端之家收集整理的这篇文章主要介绍了javascript – jquery.trim与string.trim相比前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
jQuery trim和原生 JavaScript之间有什么区别吗?

还有其他行为,安全,性能吗?

解决方法

JavaScript .trim()是在ES 5.1中定义的,不适用于IE< 9.因此,如果您已经使用jQuery,则可以使用性能较低的$.trim() jQuery的$.trim()方法
trim: function( text ) {
    return text == null ? "" : ( text + "" ).replace( rtrim,"" );
}

其中rtrim基本上就是这个RegExp

new RegExp( "^" + whitespace + "+|((?:^|[^\\\\])(?:\\\\.)*)" + whitespace + "+$","g" )

和空格表示为此组:

"[\\x20\\t\\r\\n\\f]"

JavaScript (ES6),它被定义为:

21.1.3.25 String.prototype.trim ( )

This function interprets a String value as a sequence of UTF-16 encoded code points,as described in 6.1.4.

The following steps are taken:
Let O be RequireObjectCoercible(this value).
Let S be ToString(O).
ReturnIfAbrupt(S).
Let T be a String value that is a copy of S with both leading and trailing white space removed. The definition of white space is the union of WhiteSpace and LineTerminator. When determining whether a Unicode code point is in Unicode general category “Zs”,code unit sequences are interpreted as UTF-16 encoded code point sequences as specified in 6.1.4.
Return T.
NOTE The trim function is intentionally generic; it does not require that its this value be a String object. Therefore,it can be transferred to other kinds of objects for use as a method.

所以更不用说将JS的.trim()实现给浏览器供应商了,重点很明确:从String的开头和结尾删除任何换行符或空格的表示.

原文链接:https://www.f2er.com/jquery/158997.html

猜你在找的jQuery相关文章