prototype printing undefined in console with setInterval function in javascript -


this question has answer here:

i trying print in prototype function using setinterval it's showing undefined in console.

function newfunc(){                        this.name = "this person name";             this.age = "16 years";                    }           newfunc.prototype.init = function(){                        setinterval(function(){newfunc.prototype.xyz()}, 1000);                    }                           newfunc.prototype.xyz = function(){                        console.log(this.age);                    }             var abc = new newfunc();                    abc.init();

its inside function.so apply this.xyz() .this declare variable , apply setinterval(copy.xyz())

why use this

this.xyz() applicable on newfunc.prototype.init.not ah setinteval(function (){}).these 2 function seperate. passing setinterval function .we need 1 variable.so declare var copy

function newfunc(){                        this.name = "this person name";             this.age = "16 years";                    }           newfunc.prototype.init = function(){           var copy = this;//in `this` applicable  newfunc.prototype.init.it copy variable             setinterval(function(){//in function different.               copy.xyz()//passing variable , excute function                          }, 1000);                    }                           newfunc.prototype.xyz = function(){                        console.log(this.age);                    }             var abc = new newfunc();                    abc.init();


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -