5

I am writing a script that deals with a variable gameRegion like so:

//In the main of the script

var variable= new work();
variable.onCrash(crashHandler,{xPos:650,yPos:300});


// In function work()

var gameRegion;
var onCrashCallback;

this.onCrash = function(crashCallback,fieldSize) {
gameRegion = fieldSize;
onCrashCallback = crashCallback;
};

crashHandler(){
//unimportant
}

this.atBottom = function(ypos) { 
    if(ypos>gameRegion.yPos) //line with the problem
        return true;
    return false;
};

I am getting the console error: TypeError: 'undefined' is not an object (evaluating 'gameRegion.yPos'). Presumably that means I am not properly defining gameRegion or its variable yPos. I've been looking at this code for a while now and I can't seem to find what the problem is.

Hopefully you'll see something that I don't, but if I'm not including necessary code for context, please tell me. Thanks for any help in advance.

9
  • Be careful -- class is a reserved word in JavaScript, and not to mention a terrible name for a function object. Commented Jan 7, 2014 at 5:08
  • sorry was just using a placeholder, I'll change the name of it for clarification. Commented Jan 7, 2014 at 5:09
  • There's nothing really wrong with your code, but it's hard to understand because it's not very usable as posted (you don't even have any sample code that calls the function where you're having the issue). I created a jsfiddle to get my head around it and with a little rearranging it works just fine: jsfiddle.net/consultcory/wd46G Commented Jan 7, 2014 at 5:16
  • @Cory I'd have to give a lot more context that goes beyond the scope of the question to include atBottom(), but the console leads me to believe the the error only resides in declaring and initializing of gameRegion, but you're telling me that its correct? Commented Jan 7, 2014 at 5:32
  • 1
    Good. I think what you are seeing is due to other syntax and programming errors in your JavaScript. Your fiddle doesn't compile or run. I spend about 20 minutes with it cleaning it up, but I started getting too many other errors. If you fix these one by one I think you'll find that your original problem will go away. jsfiddle.net/consultcory/vFH4c/9 Compare your version to mine to see what changes I made. Commented Jan 8, 2014 at 13:56

2 Answers 2

9

You have to handle 'undefined'. Which can be done in these ways:

typeof(foo) == 'undefined'
typeof foo !== 'undefined'
window.foo !== undefined
'foo' in window

The first three should be equivalent (as long as foo isn't shadowed by a local variable), whereas the last one will return true if the global varible is defined, but not initialized (or explicitly set to undefined).

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

Comments

2

You can use typeof like so -

return (typeof (gameRegion) !== "undefined" && 
        typeof(gameRegion.yPos) !== "undefined" &&
        ypos > gameRegion.yPos);

7 Comments

This returns false if gameRegion is undefined and prevents the problem of a console error, but doesn't solve the problem of both of those as being interpreted as undefined (I know they aren't).
Clearly they are. Do you declare gameRegion anywhere besides your onCrash function? And when does atBottom get invoked?
I could try to go into detail explaining atBottom, but then you really need more context and I'd end up having to explain the whole program. I'm sure my problem is with gameRegion and no, it is not declared anywhere else.
gameRegion has no value until after onCrash is actually invoked.
Add a console.log in onCrash to check; and maybe check the gameRegion.prototype.
|

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.