0

not sure if this is possible or if I'm doing it wrong. I'm trying to have an object literal reference one of it's own properties that is another object literal. I keep getting an error stating that it is undefined:

var MyObject = {
    init: function(){
        this.elements.buttons.click(function(){
            alert('hit');
        });
    },
    elements: {
        buttons: $('button')
    }
}

what am i doing wrong?

UPDATE: main.js is calling init to initialize on demand which probably doofed the scope --

function executeFunctionByName(functionName, context /*, args */) {
    var args = [].slice.call(arguments).splice(2);
    var namespaces = functionName.split(".");
    var func = namespaces.pop();
    for(var i = 0; i < namespaces.length; i++) {
        context = context[namespaces[i]];
    }
    return context[func].apply(this, args);
}

$(document).ready(function(){
    // invoke init() for any element that requests it
    $('[data-page]').each(function(i, p){
        var page = 'APP.MODEL.' + $(p).data('page') + '.init';
        executeFunctionByName(page, window)
    });
});
Sinaesthetic
  • 11,426
  • 28
  • 107
  • 176

1 Answers1

2

If you are invoking that function directly without going through MyObject, this would be undefined. Replace this with MyObject and it should work.

This would not work as this would be defined to window:

var MyObject = {
    // init: ...
}
var init = MyObject.init;
init();

See 3. Entering function code: How does the "this" keyword work?

If you wish to continue to use this, you may use Function.prototype.call() or Function.prototype.apply() like so:

var MyObject = {
    // init: ...
}
var init = MyObject.init;
init.call(MyObject);
Community
  • 1
  • 1
user193130
  • 8,009
  • 4
  • 36
  • 64
  • Your answer is plain wrong, this will refer to MyObject if you call if from MyObject, the problem here is that click is not defined, this has nothing to do with it – Willem D'Haeseleer Apr 21 '14 at 20:59
  • @Willem D'haeseleer Please read what I wrote. I said WITHOUT going through MyObject – user193130 Apr 21 '14 at 21:01
  • @Willem D'haeseleer Oh you're right about the click. However, the OP changed it to `buttons.click` so that's not the problem. – user193130 Apr 21 '14 at 21:03
  • Very correct, but the problem still isn't related to `this`, it's probably jquery related or something. So your answer still isn't being very helpfull. – Willem D'Haeseleer Apr 21 '14 at 21:07
  • @WillemD'haeseleer Well we still don't know what the problem is so I'll leave this up here in case it helps anyone else because a lot of people actually don't know that `this` is undefined if not called from `MyObject` as in this case. – user193130 Apr 21 '14 at 21:14
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/51131/discussion-between-willem-dhaeseleer-and-user193130) – Willem D'Haeseleer Apr 21 '14 at 21:15
  • There appears to be know discussing anything with you, you seem to think you know everything – Dexygen Apr 21 '14 at 21:31