Rouge
Rouge

Reputation: 4239

undefined variable issue

I am having trubles getting a variable declared.

I have

function employees(){
   //init employees...;
}

employees.prototype.getName=function(){
            if(ajax.doingStuff){
                return;
            }               
        } 

$(document).ready(function(){
     var ajax=new ajaxCall();
     var people=new employees();   

     $('#option').on('change', function(){
                people.getName();                
       })        

})

and when I click $('#option) button, I got

Uncaught ReferenceError: ajax is not defined

Can anyone help me to solve this problem? Thanks a lot!

Upvotes: -1

Views: 71

Answers (2)

Mike Christensen
Mike Christensen

Reputation: 91696

Your variable ajax only exists within the scope of the function passed in to ready(). If you want to use it elsewhere, you'll either have to pass a reference to that object (eg, pass in ajax as a parameter of the getName function), or move all code to the same scope.

For example:

var ajax=new ajaxCall();

Could be moved to the global scope, or:

function employees(){
   //init employees...;
}

Could be moved into the ready function.

Upvotes: 2

James Montagne
James Montagne

Reputation: 78730

You have a scope issue. The ajax variable is only available within the ready function. You either need to move the ajax variable out of the ready function or move the code which uses it in.

Upvotes: 2

Related Questions