Jump to content

value of undefined object properties


ala888

Recommended Posts

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

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

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 by ala888
Link to comment
Share on other sites

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

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

>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

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')
  • Like 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...