Home > Web Front-end > JS Tutorial > body text

Solution to overwriting JavaScript functions with the same name

黄舟
Release: 2017-03-18 15:09:26
Original
2527 people have browsed it

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]);
			}
		}
	}
}
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!