开发者

Javascript Reference Outer Object From Inner Object

Okay, I see a few references given for Java, but not javascript ( which hopefully you know is completely different ). So here's the code specific :

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[m开发者_高级运维odules[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);
}

How do I access this.config.win from within this.core.forEach? Or is this not possible?


in the body of the Sandbox() function, like this:

var self = this;

and then;

self.config.win

in the core section when the context-sensitive 'this' has changed


You can also use a function expression however this requires that you define this.config before this.core. This prevents you from having to use a local variable. Note however this captures the value instead of referencing it so if you assign a new value to config it will have no effect on forEach. You can however still change config.win since the object was not cloned. This kind of behavior can be subtle and cause bugs if used incorrectly.

With all that said it is best to use var self = this; as suggested by Jonathan. This is however another way to do it and has situations where it is useful.

this.config = {
    'win' : "win",
    'doc' : "doc"
};

this.core = {
    'exp': {
        'classParser': function (name) {
            return (new RegExp("(^| )" + name + "( |$)"));
        },
        'getParser': /^(#|\.)?([\w\-]+)$/
    },
    'typeOf': typeOf,
    'hasOwnProperty': function (obj, prop) {
        return obj.hasOwnProperty(prop);
    },
    'forEach': (function(config){
        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)
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜