jquery中,多js文件解决命名冲突

有2个js文件

test01.js

(function($){	
		test_function = function(str) {
				 alert(1+str);
		};
		test_function2= function(str) {
				 alert(2+str);
		}
})(jQuery);

test02.js

(function($){	
		test_function = function(str) {
				 alert(3+str);
		};
		test_function2= function(str) {
				 alert(4+str);
		}
})(jQuery);

调用文件test.html

<html>
	<head>
		<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
		<title>New Tab</title>
		<script src="jquery-1.6.2.min.js"></script>
		<script src="test01.js"></script>
		<script src="test02.js"></script>
		<script type="text/javascript">
		  $(function(){
			test_function("test");
			test_function2("test");
		  });
		</script>	
	</head>
	<body>
	</body>
</html>
由于test01.js和test02.js的函数重名,后载入的js方法会覆盖之前载入的同名js方法。
所以,test.html的执行结果是弹出3test和4test。

我的解决方法是:

test01.js

var test01 = {};
(function($){
	test01.func = {
		test_function:function(str) {
				 alert(1+str);
		},
		test_function2:function(str) {
				 alert(2+str);
		}
	}
})(jQuery);

test02.js

var test02 = {};
(function($){
	test02.func = {
		test_function:function(str) {
				 alert(3+str);
		},
		test_function2:function(str) {
				 alert(4+str);
		}
	}
})(jQuery);

test.html

<html>
	<head>
		<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
		<title>New Tab</title>
		<script src="jquery-1.6.2.min.js"></script>
		<script src="test01.js"></script>
		<script src="test02.js"></script>
		<script type="text/javascript">
		  $(function(){
			test01.func.test_function("test");
			test01.func.test_function2("test");
			test02.func.test_function("test");
			test02.func.test_function2("test");
		  });
		</script>	
	</head>
	<body>
	</body>
</html>

test.html的执行结果正确,没有因为test01.js和test02.js函数名相同就相互覆盖。


参数网址:

http://qilei.org/201005/how-to-make-the-func-name-no-clrash/

http://www.cnblogs.com/5201314/archive/2009/05/22/1487213.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值