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.

0

3 Answers 3

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);
    };
},

...
Sign up to request clarification or add additional context in comments.

3 Comments

I think the OP is aware of this: "However, when I do the function is invoked rather than passed and I end up with: [...]". The question is "How do I pass the function with specific parameters in this case but to ensure it is not invoked?"
Ahh ok will make update then :-)
@FelixKling I made an update.
0

You are invoking print_season.

Try something like this.

this.inner_object.inner_function(function() { this.print_season("winter") });

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.