Home >Web Front-end >JS Tutorial >Two ways to implement trim() function in Javascript_javascript skills

Two ways to implement trim() function in Javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:15:581127browse

There are many places where we need to use trim in JavaScript, but JavaScript does not have an independent trim function or method to use, so we need to write a trim function ourselves to achieve our purpose.

Option 1:

Call in prototype mode, that is, in the form of obj.trim(). This method is simple and widely used. It is defined as follows:

Copy code The code is as follows:



Usage examples are as follows:
Copy code The code is as follows:


Option 2:

Call in tool mode, that is, in the form of trim(obj). This method can be used for special processing needs. The definition is as follows:

Copy code The code is as follows:



Usage examples are as follows:
Copy code The code is as follows:


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn