user3609223
user3609223

Reputation: 3

call function not giving ouput as expected

I have developed code to understand and see the working of call function in javascript..

The code I have tried:

var animals = [{name: "ram",age:"20"}];

for(var i=0;i<animals.length;i++) {

(function(i) { console.log(this.name) }).call(animals,i);

When I tried this on the console it didn't give me the output..what I need is to return the object array animals ..

Hope you all can help me ..Thanks in advance..

Upvotes: 0

Views: 26

Answers (1)

K K
K K

Reputation: 18099

You need to modify your code like this:

var animals = [{
    name: "ram",
    age: "20"
}];

for (var i = 0; i < animals.length; i++) {
    (function (i) {
        console.log(this[i].name,this[i]);//This is the modification part.
    }).call(animals, i);
}

DEMO : http://jsfiddle.net/lotusgodkk/GCu2D/83/

Upvotes: 1

Related Questions