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.

6
  • You need to call this.func2() Commented May 15, 2015 at 12:11
  • besides you dont need to use new key word Commented May 15, 2015 at 12:12
  • @ozil i guess OP need an object in myObj not a constructor function Commented May 15, 2015 at 12:13
  • Please correct functon => function Commented May 15, 2015 at 12:13
  • @RGraham, in this case i get error "Uncaught TypeError: this.func2 is not a function" Commented May 15, 2015 at 12:13

5 Answers 5

2

Change your scripts to

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

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

   this.func2 = function () {
       ...
   };
};
Sign up to request clarification or add additional context in comments.

2 Comments

I do not see a way without instantiating myObj
@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
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();
   }

}

1 Comment

Please correct functon ==> function, and remove the new keyword.
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 () {}
};

2 Comments

Well it is syntactically correct though isn't it? Just seems a bit redundant to new a singleton
This is the most appropriate answer - some detailed reading on why you definitely shouldn't new anonymous functions: stackoverflow.com/questions/9782379/…
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();

Comments

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(){
       ...
   }
};

Comments

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.