JS implicit global variable caused by Bug sample code _javascript Tips

Source: Internet
Author: User
A piece of JS code encountered a bug, due to the implicit global variable in the middle,
Because more code, through the Google Browser JS debugger to find the problem,
In any case, my computer can not be installed on the Fiefox, from the beginning of last year to try many times, have failed to end,
But Google's debugging is also very useful.
Simplify the code as follows:
Copy Code code as follows:

$ (function () {
var pageno = 2;//This parameter is variable
var pageSize = 10;
Test ();
Paginate (pageno,pagesize)//Because the test () method overrides PageNo, PageNo always equals 1
});
function Test () {
PageNo = 1;//global variable, overwriting previous PageNo, equivalent to writing Var pageno on the top of JS = 1
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 recommended all variables are declared with Var, and all the variables can be written to the top, because JS does not block-level scope.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.