Home >Web Front-end >JS Tutorial >Bug sample code caused by js implicit global variables_javascript skills

Bug sample code caused by js implicit global variables_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:51:381416browse

A piece of js code encountered a bug, which was caused by the implicit global variable in the middle.
Due to the large amount of code, I found the problem through the js debugger of Google Chrome.
By the way, my computer is equipped with I can’t use fiefox. I have tried many times since last year but all failed.
But Google’s debugging is also very useful.
The simplified code is as follows:

Copy the code The code is as follows:

$(function() {
var pageNo = 2;//This parameter changes
var pageSize = 10;
test();
paginate(pageNo,pageSize);//Because the test() method overrides pageNo , causing pageNo to always be equal to 1
});
function test(){
pageNo = 1;//Global variable, overwriting the previous pageNo, equivalent to writing var pageNo = 1 at the top of js
//Change here to var pageNo = 1; that's it
//do,,,
}
function paginate(pageNo,pageSize){
window.location.href = "user_list.action ?pageNo=" pageNo "&pageSize=" pageSize;
}

In js, it is recommended that all variables be declared with var, and all variables can be written to the top, because js does not have blocks level scope.
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