0

I want to use a variable inside another function but is not defined! I tried it with this and var. how can I make it defined in another function? in the code below, I want to add actionInt by 1 in "next" but it is not defined. any idea?

        func1 : function(event) {

            if (action == "test"){
                console.log(test clicked);
            }


            else if (action == "test2"){

                console.log(test2 clicked);
            }



        },
5
  • Where is currentData? Commented Sep 19, 2013 at 13:33
  • remove this because it's not needed to refer to local variable Commented Sep 19, 2013 at 13:33
  • @Ian Brindley, sorry I meant actionInt Commented Sep 19, 2013 at 13:35
  • If you want your value to be "global" you need to set that var outside of your onPageClick method. When your calling this.loadPage this will refer to the actual element that raised the event and probably not your class that has the loadPage method, maybe thats your error? Commented Sep 19, 2013 at 13:43
  • You do realize that, in both cases console.log(this.actionInt++) or console.log(this.actionInt--); will log the same value, right? I mean: the statements are saying: log the current value of this.actionInt, then increment/decrement it by one... in other words: if this.actionInt is 25, you'll always log 25, then reassign the property to hold either 26 or 24 Commented Sep 23, 2013 at 6:11

2 Answers 2

1

that's because actionInt was declared in this line:

var actionInt = parseInt(action);

And you're incrementing this.actionInt++, where this is the call context, determined ad-hoc (probably the object to which you attached the event listener).
I strongly recommend you read up on what this actually references in JS, just like I'd also suggest you specify the radix when using parseInt, and realize that this doesn't make sense:

actionInt = parseInt(action, 10);//action is number?
if (action === 'next')//action is string?

if action is next or previous, parseInt will always return NaN, which, when incremented, still is NaN (Not A Number + 1 is Not A Number)

But which ever way you look at it, I think you're actually looking for a closure:

onPageClick : (function(actionInt)
{
    return function(event)
    {
        var action = event.target.innerHTML;
        this.loadPage(actionInt, 25);//what is this?
        if (action == "Next"){
            console.log(actionInt++);//no this
        }
        else if (action == "previous"){
            console.log(actionInt--);
        }
    };
}(0)),//pass initial actionInt value here

Refer to this answer on details on how JS resolves expressions/variable names and how closures work...
If actionInt is a global variable, you can omit the IIFE, and just use actionInt, it'll resolve to the global variable by default (or throw an error if it wasn't defined in strict mode)

Sign up to request clarification or add additional context in comments.

Comments

0

In your case this refers to global scope(probably) - window object.

In such case as yours there's no need to use this.variable = 0 just use var variable = value;

1 Comment

what is variable? where should I define it?

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.