admin管理员组

文章数量:1290526

As per jQuery 3.5 documentation, jQuery.trim() is deprecated.

But when I try to use it with jQuery 3.5 it's still working. Should I replace it with the native String#trim function immediately, or can I still use jQuery trim for a while?

As per jQuery 3.5 documentation, jQuery.trim() is deprecated.

But when I try to use it with jQuery 3.5 it's still working. Should I replace it with the native String#trim function immediately, or can I still use jQuery trim for a while?

Share Improve this question edited Dec 23, 2022 at 0:27 Tristan F.-R. 4,2243 gold badges25 silver badges55 bronze badges asked Aug 12, 2020 at 13:16 arunnbioarunnbio 531 gold badge1 silver badge6 bronze badges 3
  • 5 Deprecated means it's a warning it will be removed in the future, it does not mean it was removed now. – Spencer Wieczorek Commented Aug 12, 2020 at 13:17
  • And to answer your question, yes you should use the native function. – Karl-André Gagnon Commented Aug 12, 2020 at 13:18
  • String.trim works on all useful browsers – mplungjan Commented Aug 12, 2020 at 13:22
Add a ment  | 

3 Answers 3

Reset to default 6

Deprecated does not mean removed, it means that in a future version there is no guarantee that this method will still exist.

As per the jquery trim docs:

Note: This API has been deprecated in jQuery 3.5; please use the native String.prototype.trim method instead.

Docs for String.prototype.trim

Yes, it is depreciated but the suggested replacement of String.prototype.trim() can cause issues since it makes the assumption that the object is already a string.

I chose to define the following and turn all calls of $.trim(object) into String.trim(object).

String.trim = String.trim || function (value) {
    if (value === null)
        return '';
    else
        return String(value).trim();
};

It appears that there is a change in how this function is now being called. For codes using jQuery 3.5 and below, the usual function call works:

let xString = " This is my string with spaces  ";
let xTrimmed = xString.trim();

But for codes using jQuery 3.5 upwards:

let xString = " This is my string with spaces  ";
let xTrimmed = $.trim(xString);

Edit:

It appears that jQuery as gone back to its original method: xString.trim(); try any of the two that works.

本文标签: javascriptIs jQuerytrim() deprecatedStack Overflow