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

What does the 'in' keyword in javascript mean? - Stack Overflow

programmeradmin3浏览0评论

I found this code:

if (!("aa" in window)) {  
    alert('oh my god');
    var aa = 1;  
}  
alert("aa" in window);
alert(aa);

This code the second alert is alert true,but,the third alert is 'undefined',and the alert in the 'if' is not run. Why?

I think the reason is the in; what is its effect?

I searched on Google, but found nothing, because Google thinks the word ‘in&srquo; is a filter word.

We always use the in in loops, but, frankly speaking, I use it but don’t really understand it.

I found this code:

if (!("aa" in window)) {  
    alert('oh my god');
    var aa = 1;  
}  
alert("aa" in window);
alert(aa);

This code the second alert is alert true,but,the third alert is 'undefined',and the alert in the 'if' is not run. Why?

I think the reason is the in; what is its effect?

I searched on Google, but found nothing, because Google thinks the word ‘in&srquo; is a filter word.

We always use the in in loops, but, frankly speaking, I use it but don’t really understand it.

Share Improve this question edited Jun 3, 2013 at 8:51 LIXer asked May 30, 2013 at 8:56 LIXerLIXer 3621 gold badge4 silver badges11 bronze badges 4
  • 1 I guess that a variable named aa was created in the global context. alert("aa" in window); returns false for me but aa = "foo"; alert("aa" in window); returns true. – andyb Commented May 30, 2013 at 9:02
  • oh my god,i think i lost some code when i wroten the question... – LIXer Commented May 30, 2013 at 9:24
  • I think this is an entirely different question now if you are asking about alerts. I'd recommend editing the question title to a better description of your problem. – andyb Commented May 30, 2013 at 10:40
  • @LIXer I edited my answer to answer the new question but please try to fix your question immediately next time or to ask a new one when you realize the code you gave first time wasn't the right one for you. – Denys Séguret Commented May 30, 2013 at 12:44
Add a comment  | 

3 Answers 3

Reset to default 17

This tests if the window object has a property (filled or not) whose key is "aa".

This operator is very useful because it works even if the value is undefined :

window.aa = undefined; // or just aa=undefined if you're in the global scope
console.log('aa' in window); // logs true

It also works if the property isn't enumerable :

console.log('length' in []); // logs true

In your case, there may not be an aa value, but if the alert shows you true, the property was added to window.

MDN reference on in

Note that the for...in statement is different in that it doesn't really use the in operator but is a specific construct.

MDN reference on for...in


EDIT : an explanation of your edited question (very different from the first one) :

Your confusion seems to arise from the fact you declared the var aa = 1; in a block. You should know that the scope of a variable in JavaScript is either a function of the global scope and that declarations are hoisted. So your code is in fact equivalent to

var aa = undefined;
if (!("aa" in window)) { // aa is in window, so we don't enter here
    alert('oh my god');
    aa = 1;  
}  
alert("aa" in window); // yes, the property exists, it's true
alert(aa); // aa is still undefined

Taking the alerts in order:

  • alert #1 is never reached because ("aa" in window) === true so the if boolean condition is false.

JavaScript has function scope and the variable aa is "hoisted" up to the top of the scope first, so it is defined.

  • alert #2

"aa" in window is true because the variable was added to the window object when it was hoisted up. Equivalent to just writing:

var foo;
"foo" in window (which === true)
  • alert #3

From the Standard ECMA-262 ECMAScript Language Specification :

A variable statement declares variables that are created as defined in 10.5. Variables are initialised to undefined when created. A variable with an Initialiser is assigned the value of its AssignmentExpression when the VariableStatement is executed, not when the variable is created.

So aa is undefined since the assignment was never executed.

in checks if property exists in Object

// in the below snippet they are checking if 'aa' property exists in 'window' Object . Since variables are function declarations are hoisted. you'll never enter inside if block .

var aa = undefined ;
if (!("aa" in window)) {   //2 => 'aa' property exists 
    alert('oh my god');  
    aa = 1;         // 1 =>  hoisted 
}  
发布评论

评论列表(0)

  1. 暂无评论