ala888 Posted July 2, 2014 Share Posted July 2, 2014 Can someone explain this phenomena to me? or atleast tell me what its called?Also, where can I get a comprehensive list of these object things in Js?Im stumbling across stuff like ".name" on accident, but no idea where the "official" properties and objects come from document.forms.random == undefined document.random == undefined document.forms.random.qwe == ???? documents.random.qwe == ???? Link to comment Share on other sites More sharing options...
justsomeguy Posted July 2, 2014 Share Posted July 2, 2014 If you're talking about the DOM, there's a reference here:https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_ModelYou can start with the document object and go from there. The forms collection is here, for example:https://developer.mozilla.org/en-US/docs/Web/API/Document.formsNote that different browsers do different things. If you have a form with the name "random", some browsers would put that in document.forms.random, others might make you access it with an index. Link to comment Share on other sites More sharing options...
ala888 Posted July 2, 2014 Author Share Posted July 2, 2014 (edited) no specifically, I mean how the value of a couple drop into complete void, IDK what their value is, even if I use "else", like if it aint x==2, it does not work ! So like, at what level of "undefined" does the ????? come in. Sorry, I have no idea how to phrase this without being super obscure document.forms.random == undefined document.random == undefined document.forms.random.qwe == ???? documents.random.qwe == ???? Edited July 2, 2014 by ala888 Link to comment Share on other sites More sharing options...
justsomeguy Posted July 2, 2014 Share Posted July 2, 2014 If a variable is undefined then the value is undefined. Undefined is a special value. If you try to access any property of an object that is undefined, that is a runtime error. If you think an object might be undefined you should check for that before trying to access properties of it, or else you'll get an error along the lines of "cannot access property of undefined". Link to comment Share on other sites More sharing options...
ala888 Posted July 2, 2014 Author Share Posted July 2, 2014 why is there an error? Why not simply return "undefined" like usual Link to comment Share on other sites More sharing options...
justsomeguy Posted July 2, 2014 Share Posted July 2, 2014 I'm not sure if that's part of the specification, or whether that's just what browser vendors did. I'm also not sure if all browsers behave that way, but I've seen that error plenty of times. Link to comment Share on other sites More sharing options...
Hadien Posted July 2, 2014 Share Posted July 2, 2014 because "undefined" doesn't have properties. it's just simply... undefined. Go pick up any random book. a book is an "object", it has a collection of data, or pages, full of letters. You can go to page 32 and tell me the 1st letter on that page. and you can follow along cause I told you to pick up any random book. but now lets say I want you to pick up "the" book, and tell me the last letter on page 52. Well, I never told you what "the" book was so how can you possibly know that page 52 even exists and that it even has any letters on it at all? You don't know where to start because the next step of instructions can't be followed because a previous step failed; obtaining "the" book. All following steps are moot, because it all depends on that first step. In this case, page 52 is not strictly a "undefined" value, its a value that cannot be determined. You can't know if it is even undefined, if it even exists. and Javascript doesn't know how to handle that. On the other hand, "cannot access property of undefined", is far more informational in finding buggy code than just simply having it return "undefined" Link to comment Share on other sites More sharing options...
ala888 Posted July 2, 2014 Author Share Posted July 2, 2014 >go pick up "the" book which does not exist >cant find it, return undefined.>go to page 52>cant find it, but return an error instead of just inheriting the undefined value good job developers. Link to comment Share on other sites More sharing options...
justsomeguy Posted July 2, 2014 Share Posted July 2, 2014 It's true what the error message says though - you cannot access a property of undefined. That's because undefined has no properties to access.If you have an object called obj which is undefined, and you're trying to access obj.prop1.prop2.prop3, then there is probably an error in your logic that you need to be aware of. Having it return the value undefined and letting your code just go on wouldn't necessarily be helpful in figuring out what the problem is.If you want to check if something is undefined, I usually use this: if ((typeof obj) == 'undefined') 1 Link to comment Share on other sites More sharing options...
ala888 Posted July 2, 2014 Author Share Posted July 2, 2014 thanks for the tip, found it really helps Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now