2

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.

1
  • 1
    Who is voting to close this as a typo? Misunderstandings aren't typos. Commented Oct 6, 2024 at 20:52

4 Answers 4

5

Get rid of the this.name inside the function. It is not referencing the function, but instead the window object; that is, this.name is not the variable name inside the function.

this.getPageName = function(pagenum){

    var name;

    if(pagenum==1){
        name='1.page1';
    }
    else if(pagenum==2){
        name='2.page2';
    }
    else if(pagenum==3){
        name='3.page3';
    }
    else if(pagenum==4){
        name='4.page4';
    }
    console.log("pagenum: " + pagenum);
    console.log("tabname: " + name);
    return name;

}

var page=3;

var pagename=getPageName(page);

console.log(pagename);
Sign up to request clarification or add additional context in comments.

Comments

1

All the this is messing you up. Specifically, you're defining the function as this.getPageName and then attempting to call the function getPageName, which you haven't defined yet - you defined this.getPageName. Even if the code does run that way, it's unnecessary. If the console.logs inside the function are not executing, then the function isn't running at all.

Then, inside the function, you're referencing this.name when name alone will do. Remove every instance of this from your code and it should work.

You could also tighten up the function by changing your code to this:

getPageName = function(pagenum) {
    // Start with '' to make sure it interprets pagenum as a string, not a digit
    var name = '' + pagenum + '.page' + pagenum;

    console.log("pagenum: " + pagenum);
    console.log("tabname: " + name);

    return name;

}
var page=3;
var pagename=getPageName(page);
console.log(pagename);

Comments

1

Either use it like an object

var pagename=new getPageName(page);
console.log(pagename.name);

DEMO.

Or just remove the this keyword from all this.????='' from inside the function. Also, you can use var getPageName = function(){ //... }; instead of this.getPageName.

DEMO.

Comments

1

You don't need to add the keyword this. You defined name at the same scope as the rest of the function. That should resolve the issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.