js全局配置文件
配置文件能够大大提高代码的可复用性,把一些变量提取出来,也可以方便代码的维护,需要改个参数、地址之类的直接到配置文件里面改就行了,不用找到具体的实现代码。
var config = (function($) {
$.Obj = {
contextPath:'http://localhost:8080/test',
maps: [
{id:'map1',url:'*********'},
{id:'map2',url:'*********'}
]
}
return $;
})(window.config || {});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="config.js"></script>
</head>
<body>
<script>
console.log(config.Obj.contextPath,'------');
</script>
</body>
</html>
调用contextPath
,直接config.Obj.contextPath
就拿到了。