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

javascript - Checking if a property exists in "window" fails if "window." is not prepended -

programmeradmin3浏览0评论

I want to check if a property exists in window, so I did this:

This code works:

if (window.foo) {
    alert("exists");
}
else {
    alert("doesn't exist");
}

Output:

doesn't exist

I thought the next code should work too because as far as I know when you define variables and you are not inside a function they are created as a property of the "window" object so this should be equivalent:

if (foo) { //it fails here "foo is not defined"
    alert("exists");
} else {
    alert("doesn't exist");
}

To my surprise it didn't work. My question is why it doesn't work if I don't prepend window?

I want to check if a property exists in window, so I did this:

This code works:

if (window.foo) {
    alert("exists");
}
else {
    alert("doesn't exist");
}

Output:

doesn't exist

I thought the next code should work too because as far as I know when you define variables and you are not inside a function they are created as a property of the "window" object so this should be equivalent:

if (foo) { //it fails here "foo is not defined"
    alert("exists");
} else {
    alert("doesn't exist");
}

To my surprise it didn't work. My question is why it doesn't work if I don't prepend window?

Share Improve this question edited Jul 3, 2013 at 22:23 Alfredo Osorio asked Jul 3, 2013 at 22:17 Alfredo OsorioAlfredo Osorio 11.5k12 gold badges59 silver badges85 bronze badges 5
  • 1 Where did you see that variables not defined in a function are automatically added to the window object? – Mike Commented Jul 3, 2013 at 22:19
  • 3 if (typeof foo !== 'undefined') – elclanrs Commented Jul 3, 2013 at 22:19
  • 1 @Mike var myName = "Alfredo Osorio"; alert(window.myName); //Alfredo Osorio – Alfredo Osorio Commented Jul 3, 2013 at 22:20
  • So what's your question? – fehnomenal Commented Jul 3, 2013 at 22:22
  • @BenjaminGruenbaum nice that should be the answer. – Alfredo Osorio Commented Jul 3, 2013 at 22:25
Add a ment  | 

2 Answers 2

Reset to default 7

The first version is property access, it can't find the property so it returns undefined. The second is trying to access undefined variable .

Use what elclanrs suggested:

if(typeof foo !== "undefined"){
    alert("exists");
} else {
    alert("doesn't exist");
}

Why?

This is because of the GetValue specified in the language specification.

.3. If IsUnresolvableReference(V), throw a ReferenceError exception.

This is what happens in the if(foo) case, since foo was not defined before.

On the other hand, if its an object the following happens:

  1. Let base be the result of calling GetBase(V).
  2. Let O be ToObject(base).
  3. Let desc be the result of calling the [[GetProperty]] internal method of O with property name P.
  4. If desc is undefined, return undefined.

So window.foo returns the primitive language value undefined which is falsy.

Think of the Window object as a "giant closure", which encapsulates all the others. Now think about this:

var ext = "test";
function myScope() {
    var ext = "foo"; //new variable in myScope's scope
    console.log(ext); //foo
}
function changeExt() {
    ext = "bar";//ext is not defined in this scope, so JS will look in the chain of scopes and set the value to the defined var in the global scope
    console.log(ext);
}
myScope();
console.log(ext); //test
changeExt();
console.log(ext); //ext has been changed by changeExt to bar

Why is this happening? Because by using window.property you're trying to access an undefined property of the window object, not referencing to an undeclared var.

If you try to do the same thing without using the var keyword in the global closure you will get the same result, because it is the last of the scopes chain.

Now let's think about it like a giant object:

var myObj = {prop: 'defined'};

Assuming that myObj is the "global object", if you try to access prop JS will redirect you to myObj.prop(or it will try to do this), if the property (named for example) foo isn't declared you will get your ReferenceError.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论