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就拿到了。

posted @ 2022-12-06 22:18  轻风细雨_林木木  阅读(21)  评论(0编辑  收藏  举报