user4069489
user4069489

Reputation:

Javascript nested function is not worked

I do not understand why the feature does not work , I passed into the method foreach.

 var methods = {
            foreach: function(f){
                for(var i = 0; i <= this.x; i++){
                    f(i);
                }
            }
        };
        function test(x) {
           var t = Object.create(methods);
           t.x = x;
           return t;
        };
        var t = test(10);
        console.log(t.x); //10
        t.foreach(console.log()); //Uncaught TypeError: undefined is not a function

Thx!

Upvotes: 0

Views: 89

Answers (1)

Quentin
Quentin

Reputation: 944016

You are passing the return value of calling console.log(), which isn't a function.

You need to pass an actual function.

Since log only works in the context of console you can't just pass console.log but you could, for instance:

 t.foreach(function (logthis) { console.log(logthis); });

Upvotes: 1

Related Questions