命名空间:命名空间有助于减少程序中所需要的全局变量的数量,并且同时还有助于避免命名冲突或过长的名字前缀。

 

板栗:

var MYAPP = MYAPP || {};
  MYAPP.namespace = function(ns_string){
  var parts = ns_string.split('.'),
  parent = MYAPP,
  i;

  if( parts[0] === 'MYAPP'){
    parts = parts.slice(1);
  }
  for(var i=0; i<parts.length; i++){
    //不存在,就创建属性
    if( typeof parent[parts[i]] === 'undefined' ){
      parent[parts[i]] = {};
    }
    parent = parent[parts[i]];
  }
  return parent;
}
MYAPP.namespace('modele.modele.getName');
console.log(MYAPP);

javascript命名空间

相关文章:

  • 2022-12-23
  • 2021-10-23
  • 2021-09-06
  • 2022-12-23
  • 2022-01-11
  • 2022-01-04
  • 2021-10-04
  • 2022-01-02
猜你喜欢
  • 2021-11-10
  • 2021-08-03
  • 2021-10-09
  • 2021-11-04
  • 2022-12-23
相关资源
相似解决方案