0

I want to access foo.settings.debug object from inside the success listener of the require() function. I keep getting an undefined error and when I reference "this" it ends up referencing the ajax object. Please assist.

var foo = {

    info: {

        version: '0.0.1'

    },

    settings: {

        debug: true

    },

    require: function(script) {

        $.ajax({
            url: script,
            dataType: "script",
            async: false,
            success: function(){
                if(foo.settings.debug) console.log('loaded js file: ' + script);
            },
            error: function(){
                throw new Error("Could not load script " + script);
            }
        });

    }
}

2 Answers 2

1

You need a reference to foo which is this inside of foo however this is going to be something else inside of your closure so you need to keep a reference to this like so:

require: function(script) {
        var self = this;
        $.ajax({
            url: script,
            dataType: "script",
            async: false,
            success: function(){
                if(self.settings.debug) console.log('loaded js file: ' + script);
            },
            error: function(){
                throw new Error("Could not load script " + script);
            }
        });

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

1 Comment

brain fart - I was doing that but at the level above that inside the foo declaration. thanks for your help.
1

Every function has its own this object context. The context can be changed by using apply or other such functions.

In this case by creating an anonymous function as a callback for success you are entering a new context. In order to access the previous context you need to define a variable inside the previous context (with a name that will not be overridden by argument names of the callback function).

var foo = {

    info: {

        version: '0.0.1'

    },

    settings: {

        debug: true

    },

    require: function(script) {
        /* every function has its own 'this' context */
        var self = this;

        $.ajax({
            url: script,
            dataType: "script",
            async: false,
            success: function(){
                if(self.settings.debug) console.log('loaded js file: ' + script);
            },
            error: function(){
                throw new Error("Could not load script " + script);
            }
        });

    }
}

2 Comments

Tiny bug: self.settings.debug not self.debug
Yes indeed, I initially wrote self.settings.debug but thought that require is defined in foo.settings.require meaning that it would be attached to foo.require. My mistake, require is defined in foo.require and attached to foo.

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.