1

I'm am using module js syntax and I have some code like this:

var myModule = {

    settings: {
        myage: 25
    },

    init: function() {

        //init code here

    },

    someFunction1: function(param1) {

        //function code here

    },

    someFunction2: function() {

       myModule.someFunction1(myparam);

    }

}

The above will work fine but if I tried:

someFunction2: function() {

    this.someFunction1(myparam);

}

It will not find the function.

Can't I use this.someFunction1... ?

  • Possible duplicate of [How does "this" keyword work within a function?](http://stackoverflow.com/questions/133973/how-does-this-keyword-work-within-a-function) – Robert Moskal Apr 02 '17 at 11:58
  • 2
    Show us how you are calling `someFunction2` – Bergi Apr 02 '17 at 12:02
  • From outside I'm calling it like this: myModule.someFunction2(); From inside I just thought you could do this.someFunction2(); but apparently you can't ? –  Apr 02 '17 at 12:45
  • @JakeBrown777 No, if from outside you always call the methods on the module, then inside you should be able to use `this`. It appears there is a call somewhere in your code where you don't use `myModule.someFunction2()`. It might help to post a [MCVE] based on your real code – Bergi Apr 02 '17 at 12:58
  • The only place where I use THIS is in INIT function to access the settings where I use: this.settings.myage which works fine –  Apr 02 '17 at 13:11

1 Answers1

0

this is who called the function.

myModule.someFunction2() will have this=myModule

someFunction2 = myModule.someFunction2; someFunction2() will have this=window

(you didn't show how you're calling it though)

Farzher
  • 13,934
  • 21
  • 69
  • 100
  • From outside I'm calling it like this: myModule.someFunction2(); I just thought that once you are inside you could just use this that's all –  Apr 02 '17 at 12:43