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?

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

4 Comments

nitpicking, but that's not what a closure is.
Much obliged, I tried that everywhere except the Sandbox function ( long day at work probably didn't help ).
I've seen that used a lot. var that = this;.
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
1

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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.