Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript reference to undefined property

Using Firefox, working on a Firefox extension, I continually get a javascript warning:

reference to undefined property mySidebar.context.netProgress

I have tried multiple ways of testing the value:

if (mySidebar.context.netProgress === undefined) {

And

if (typeof mySidebar.context.netProgress == "undefined") {

And

if (!mySidebar.context.netProgress) {

And

if (mySidebar.context.netProgress == undefined) {

However the error console in Firefox continues to give me the warning on the same line every time, the line in question is the line that I posted the code from above. The actual check for the value is causing the warning.

I also put an alert to check the value of mySidebar.context, which is always an object, so it is not from the parent that I'm getting the warning.

Any ideas?

like image 668
Purge Avatar asked Sep 24 '10 16:09

Purge


People also ask

What is undefined property in JavaScript?

The undefined property indicates that a variable has not been assigned a value, or not declared at all.

Is undefined == undefined JavaScript?

Simply put, undefined means a variable has been declared but has not yet been assigned a value. undefined is a type by itself (undefined). Unassigned variables are initialized by JavaScript with a default value of undefined.

How do I get undefined in JavaScript?

You will get undefined value when you call a non-existent property or method of an object. In the above example, a function Sum does not return any result but still we try to assign its resulted value to a variable. So in this case, result will be undefined.


1 Answers

As Swingley said, you can use Object.prototype.hasOwnProperty() to check for existence of a direct property on an object. This won't work for properties inherited from the prototype chain, however. For both situations, inherited and direct, you can use the in operator:

if ("netProgress" in mySidebar.context) {
like image 90
Andy E Avatar answered Oct 13 '22 00:10

Andy E