0

I'm doing this but it isn't working:

function timer(func){
   //Timer code here
   ...
   func();
}

function doSomething(){
   var param1;
   var param2;
   var execute = function(){
      alert(param1 + " - " +param2);
   };
}

var instance = new doSomething();
instance.param1 = "Hi";
instance.param2 = "Test";
timer(instance.execute);

Why isn't my instance function "execute" executing inside the timer function? I got the following error: Uncaught TypeError: func is not a function

What would be the correct way?

ProtectedVoid
  • 1,293
  • 3
  • 17
  • 42
  • You also will need to understand the difference between variables and properties, e.g [here](http://stackoverflow.com/q/13418669/1048572) – Bergi Aug 21 '15 at 11:49

1 Answers1

1

you have lot's of error's in your code,I think this is what you trying to do

      function timer(func){

           func();
        }

//       following your constructor  
        function doSomething(){
          var outer = this; //'this refer  to current object'
           this.execute = function(){
              alert (outer.param1 + " - " +outer.param2); // its a closure function  
           };

          return this;
        }

        var instance = new doSomething();
        instance.param1 = "Hi";
        instance.param2 = "Test";
        timer(instance.execute);
Shailendra Sharma
  • 6,976
  • 2
  • 28
  • 48