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

javascript - Chrome console: difference between 'let' and 'var'? - Stack Overflow

programmeradmin3浏览0评论

I've attached an animated gif to illustrate this weird behavior. Essentially, my question is does Chrome console treat var and let differently when used in the same scope? You'll notice that after declaring / assigning a variable, if you try to type that variable's name into the console, Chrome will autoplete it for you, showing a dropdown list containing what your typing. When using lets, this is not the case. Is this a bug, feature, or is there something I'm missing about var and let in JavaScript?

Note: I'm well aware that let lives & dies within the immediate scope.

I've attached an animated gif to illustrate this weird behavior. Essentially, my question is does Chrome console treat var and let differently when used in the same scope? You'll notice that after declaring / assigning a variable, if you try to type that variable's name into the console, Chrome will autoplete it for you, showing a dropdown list containing what your typing. When using lets, this is not the case. Is this a bug, feature, or is there something I'm missing about var and let in JavaScript?

Note: I'm well aware that let lives & dies within the immediate scope.

Share Improve this question asked Sep 8, 2016 at 17:39 The QodesmithThe Qodesmith 3,3855 gold badges33 silver badges46 bronze badges 4
  • 3 Without being a member of the chrome development team, I'd say a) they only look for the global scope variables in their autopletion, or b) didn't care implement it for let yet, or c) they forgot to implement it – baao Commented Sep 8, 2016 at 17:46
  • 3 Just out of curiosity, how did you make that gif image? – Orkhan Alikhanov Commented Sep 8, 2016 at 17:48
  • @OrkhanAlikhanov yeeeees. That's the most interesting part of the question!!! – baao Commented Sep 8, 2016 at 17:49
  • 1 @OrkhanAlikhanov... hahaha. It's done with an age-old trusty program from the good folks over at Cockos called LICEcap. Enjoy! – The Qodesmith Commented Sep 8, 2016 at 22:51
Add a ment  | 

2 Answers 2

Reset to default 9

When you use var in the console, it executes in the global scope and adds the variable to the window object.

When you use let in the console, it executes in the global scope, which doesn't add the variable to the window object.

When you start typing, autoplete checks the parent object for properties to plete along with other language constructs, such as function, for, and while.

When there is no content in the console, the parent object is window, which won't have the property you're looking for because let doesn't add the property to window.

As soon as you have a new object for autoplete to plete, behavior returns to what you'd expect.

> let foo = {bar: 'baz'};
> foo.b //autopletes bar

Now, with all of that said, there's no reason that autoplete has to behave that way. In many regards the lack of autoplete for variables defined in global scope via let could be considered a "bug" worth "fixing". In my opinion it is moderately surprising behavior.

var defines a variable on the global scope, while let defines it only in the local scope. Most likely, the autoplete is only looking on the global scope for targets.

发布评论

评论列表(0)

  1. 暂无评论