0

In the following code, I want pass a reference to the print_season function with parameter "winter" into the function inner_function.

tony = {
    print_season: function (season) {
        console.log(">>season is" + season);
    },

    report: function () {
        console.log(">>report");
        this.inner_object.inner_function(this.print_season("winter"));
    }, 

    inner_object: {
        inner_function: function(callback) {
            console.log(">>inner_function=" + callback());
        }
    }
}

tony.report();

However, when I do the function is invoked rather than passed and I end up with:

TypeError: callback is not a function
    console.log(">>inner_function=" + callback());

How do I pass the function with specific parameters in this case but to ensure it is not invoked?

Thanks.

Breako Breako
  • 6,353
  • 14
  • 40
  • 59

3 Answers3

1

You are not passing a function.

You are actually just passing undefined


You might want print_season to return a callback function:

...

print_season: function (season) {
    // return a callback function
    return function() {
        console.log(">>season is" + season);
    };
},

...
qwertynl
  • 3,912
  • 1
  • 21
  • 43
0

You are invoking print_season.

Try something like this.

this.inner_object.inner_function(function() { this.print_season("winter") });
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
0

Try this:

tony = {
print_season: function (season) {
    console.log(">>season is" + season);
},

report: function () {
    console.log(">>report");
    this.inner_object.inner_function(function(){this.print_season("winter")});
}, 

inner_object: {
    inner_function: function(callback) {
        console.log(">>inner_function=" + callback());
    }
}
}
tony.report();

Or this:

tony = {
print_season: function (season) {
 return function(){
     console.log(">>season is" + season);
 }
},

report: function () {
    console.log(">>report");
    this.inner_object.inner_function(this.print_season("winter"));
}, 

inner_object: {
    inner_function: function(callback) {
        console.log(">>inner_function=" + callback());
    }
}
}
tony.report();

The goal is to have a function (callback) not something else.

Charlie Affumigato
  • 1,017
  • 1
  • 7
  • 10