Home  >  Article  >  Web Front-end  >  Solution to overwriting JavaScript functions with the same name

Solution to overwriting JavaScript functions with the same name

黄舟
黄舟Original
2017-03-18 15:09:262530browse

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(&#39;test is ok!&#39;);
				}
				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!

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