好吧,我看到一些Java给出的参考,但没有JavaScript(希望你知道是完全不同的)。所以这是代码特定的:Javascript参考外部对象来自内部对象
function Sandbox() {
var args = Array.prototype.slice.call(arguments)
, callback = args.pop()
, modules = (args[0] && typeof args[0] === 'string' ? args : args[0])
, i;
if (!(this instanceof Sandbox)) {
return new Sandbox(modules, callback);
}
if (!modules || modules[0] === '*') {
modules = [];
for (i in Sandbox.modules) {
if (Sandbox.modules.hasOwnProperty(i)) {
modules.push(i);
}
}
}
for (i = 0; i < modules.length; i++) {
Sandbox.modules[modules[i]](this);
}
this.core = {
'exp': {
'classParser': function (name) {
return (new RegExp("(^|)" + name + "(|$)"));
},
'getParser': /^(#|\.)?([\w\-]+)$/
},
'typeOf': typeOf,
'hasOwnProperty': function (obj, prop) {
return obj.hasOwnProperty(prop);
},
'forEach': function (arr, fn, scope) {
scope = scope || config.win;
for (var i = 0, j = arr.length; i < j; i++) {
fn.call(scope, arr[i], i, arr);
}
}
};
this.config = {
'win' : win,
'doc' : doc
};
callback(this);
}
如何从this.core.forEach中访问this.config.win?或者这是不可能的?
在沙盒()函数的主体
挑剔,但这不是闭包。 – nickf 2010-05-31 01:01:20
非常感谢,除了沙盒功能之外,我尝试过在任何地方(工作中的漫长一天可能没有帮助)。 – Akidi 2010-05-31 01:16:59
我见过'那个'用了很多。 'var that = this;'。 – gradbot 2010-05-31 01:56:32