javascript沙箱模式
沙箱模式解决了命名空间模式的如下几个缺点: 1.对单个全局变量的依赖变成了应用程序的全局变量依赖。在命名空间模式中,是没有办法使同一个应用程序或库的2个版本运行在同一个页面中。 2.对这种以点分割的名字来说,需要输入更长的字符,并且在运行时需要解析更长的时间,比如MYAPP.utilities.array 顾名思义,沙箱模式提供了一个可用于模块运行的环境,且不会对其他模块和个人沙箱造成任何影响。 Sanbox.modules = {}; Sanbox.modules.array = function(box){ var array_string = '[object Array]', opt = Object.prototype.toString; box.isArray = function(a){ return opt.call(a) === array_string; } } Sanbox.modules.object = function(box){ var obj_string = '[object Object]', opt = Object.prototype.toString; box.isObject = function(a){ return opt.call(a) === obj_string; } }