因项目需要,要求对JS的namespace进行管理,于是上网寻找相关资料,在mootools的相关论坛上找了个:
if(typeof Mootools == "undefined") { var Mootools = { Version: "0.1" }; }; Mootools.namespace = function() { var a = arguments, o = null, i, j, d; for(i=0; i<a.length; ++i) { d = a[i].split("."); o = Mootools; for(j=(d[0]=="Mootools")?1:0; j<d.length; ++j) { o[d[j]]=o[d[j]] || {}; o=o[d[j]]; } } return o; }; Mootools.namespace("util", "widget", "example"); // Creating a class Mootools.widget.widgetName = new Class({ options: { // options goes here }, initialize: function(options) { this.setOptions(options); // initialize commands goes here } }); Mootools.widget.widgetName.implement(new Events); // Implements addEvent(type, fn), fireEvent(type, [args], delay) and removeEvent(type, fn) Mootools.widget.widgetName.implement(new Options);// Implements setOptions(defaults, options)
大家现在用怎样的方法来实现JS的命名空间问题的?能否放出来分享下?