1

I create object var myObj = new functon () {...}. In that object i add functions like :

var myObj = new function () {
   this.func1 = function() {
       func2();
   }
   this.func2 = function() {
       ...
   }
}

As you can see in func1 I try to call func2 but it is always undefined. Why? Cause everything is in one object.

myTerminal
  • 1,596
  • 1
  • 14
  • 31
demo
  • 6,038
  • 19
  • 75
  • 149

5 Answers5

1

You should call func2 like this

var myObj = new function () {
   this.func1 = function () {
       this.func2();
   }
   this.func2 = function () {
     console.log('func2');
   }
}

myObj.func1();

if you want call func2 with this. and without, you can do it like this

var myObj = new function () {
    function func2() {
        console.log('func2');
    } 
    
   this.func1 = function() {
       this.func2();
       func2();
   }
   
   this.func2 = func2;
}

myObj.func1();
Oleksandr T.
  • 76,493
  • 17
  • 173
  • 144
1

Change your scripts to

var myObj = function () {
   var self = this;

   this.func1 = function () {
       self.func2();
   };

   this.func2 = function () {
       ...
   };
};
myTerminal
  • 1,596
  • 1
  • 14
  • 31
  • I do not see a way without instantiating `myObj` – myTerminal May 15 '15 at 12:38
  • @Alexander, on page i call func1 like : `SessionTimeOut.initSessionTimeOutLogic('TimeOutMessage', 'lnkBtnSave', redirect_url, sessionKeep_alive_url, session_timeout_seconds, session_timeout_warning_seconds, session_timeout_check_interval_seconds);` and in it i call also func2 – demo May 15 '15 at 12:49
1

It's undefined because you don't have local variable func2. So correct reference should be this.func2().

However even in this case your code is not ideal construction object like this (mixing constructor and anonymous function) (although correct). In this case it's better to use object literal in the first place rather then create constructor function for just creating one single object instance:

var myObj = {

    func1: function () {
        this.func2();
    },

    func2: function () {}
};
dfsq
  • 191,768
  • 25
  • 236
  • 258
  • Well it is *syntactically* correct though isn't it? Just seems a bit redundant to `new` a singleton – CodingIntrigue May 15 '15 at 12:18
  • This is the most appropriate answer - some detailed reading on why you definitely shouldn't new anonymous functions: http://stackoverflow.com/questions/9782379/deathmatch-self-executing-anonymous-function-vs-new-function – CodingIntrigue May 15 '15 at 12:28
1

On top of solutions provided by others. If you are going to call a javascript function that is defined like this

var func = function(){}

the function definition needs to come before the function call.

In the other way of defining a function this does not matter.

function func(){}

So Overall Code should be

var myObj = function(){

   this.func2 = function(){
       ...
   }
   this.func1 = function(){
       func2();
   }

}
Riddler
  • 566
  • 2
  • 10
0

you can call like this. Calling func2() directly, searches the function of window object.

var myObj = functon(){
   var current = this;
   this.func1 = function(){
       current.func2();
   }
   this.func2 = function(){
       ...
   }
};
sudhansu63
  • 6,025
  • 4
  • 39
  • 52