views:

50

answers:

2

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[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);
}

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

+3  A: 

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

Jonathan Fingland
nitpicking, but that's not what a closure is.
nickf
Much obliged, I tried that everywhere except the Sandbox function ( long day at work probably didn't help ).
Akidi
I've seen `that` used a lot. `var that = this;`.
gradbot
you're right nickf. I was originally writing something else ('var Sandbox = function (){...return function() {} }()') however, realized it wasn't necessary. thanks for the assist
Jonathan Fingland
+1  A: 

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)
}
gradbot
Although it wouldn't be useful for what I am doing, rated up since I appreciate the alternative way to handle it ( never know it may come in handy ). Much obliged to the answer sir.
Akidi