This article mainly introduces the information about variable initialization in js. If you need it, you can refer to the function that I wrote in js to remove spaces at the beginning and end and specific characters. The Code is as follows:
The Code is as follows:
Function trim (str, charlist ){
Return str. replace (new RegExp ('^ [\ s' + charlist +'] + | [\ s' + charlist + '] + $', 'G '), '');
}
The Code does not seem to have any problems, and it does not run correctly.
Today, when I searched for "note3" in the search box, I found that the content searched in the address bar is changed to "ote3", while it is normal for other letters or numbers to start, what to search and what to search in the address bar.
After debugging the code, change the code:
The Code is as follows:
Function trim (str, charlist ){
Var charlist = charlist | "";
Return str. replace (new RegExp ('^ [\ s' + charlist +'] + | [\ s' + charlist + '] + $', 'G '), '');
}
Added charlist initialization in the function. In the next search for "note3", the result is correct.
Although Javascript is a weak type language, Initialization is not required to use variables. During running, the code is automatically converted and assigned a value. However, this will cause some unexpected problems. Therefore, it is necessary to initialize all the variables used.
This is my first visit today. I hope my friends will like it.