In the JavaScript script, if the local function has the same name as the peripheral function, the external network function will be overwritten, that is, the variable can be defined repeatedly.
See examples below.
A = function(){ var me = this; me.method1 = function(){ var items = [1,2,3,4,5]; for(var i=0;i<items.length;i++){ if(1){ var items = [6,7,8]; if(items.length == 0){ alert('test is ok!'); } alert(items[i]); } } } }
The definition of local variable items is as follows:
var items = [5,6,7];
will override the definition of external network variables:
var items=[1,2,3,4,5];
Loop can only be executed 3 times.
The solution is to use different function names to avoid functions with the same name.
The above is the detailed content of Solution to overwriting JavaScript functions with the same name. For more information, please follow other related articles on the PHP Chinese website!