最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript: "Undefined" is not an object - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Jan 7, 2014 at 5:09 Wold asked Jan 7, 2014 at 5:03 WoldWold 9721 gold badge13 silver badges25 bronze badges 9
  • Be careful -- class is a reserved word in JavaScript, and not to mention a terrible name for a function object. – Cᴏʀʏ Commented Jan 7, 2014 at 5:08
  • sorry was just using a placeholder, I'll change the name of it for clarification. – Wold 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 – Cᴏʀʏ 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? – Wold 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. – Cᴏʀʏ Commented Jan 8, 2014 at 13:56
 |  Show 4 more comments

2 Answers 2

Reset to default 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).

You can use typeof like so -

return (typeof (gameRegion) !== "undefined" && 
        typeof(gameRegion.yPos) !== "undefined" &&
        ypos > gameRegion.yPos);
发布评论

评论列表(0)

  1. 暂无评论