Thursday, 5 September 2013

why below javascript function does not work?

why below javascript function does not work?

I know this is a really silly questions, but just have no idea why this
doesn't work:
this.getPageName = function(pagenum){
var name;
if(pagenum==1){
this.name='1.page1';
}
else if(pagenum==2){
this.name='2.page2';
}
else if(pagenum==3){
this.name='3.page3';
}
else if(pagenum==4){
this.name='4.page4';
}
console.log("pagenum: " + pagenum);
console.log("tabname: " + name);
return name;
}
var page=3;
var pagename=getPageName(page);
console.log(pagename);
I suppose console.log(pagename); would have 3.page3, but output is undefined.
Anything wrong? there must be some scope error there.

No comments:

Post a Comment