Creating a global for variable. Must be "for (var items ..."

I am using jshint. Can someone tell me why it treats the "for" keyword as a global variable?

Creating a global for variable. Must be "for (var items ..."

Here's the loop:

//items and properties are defined above... var items = null, properties = someObject; //code here is properly terminated with ; "semicolon" for (items in properties) { if (properties.hasOwnProperty(items)) { //some code here... } } 
+4
source share
3 answers

This is not related to for , but with items inside the for construct.

If you go like

 for (items in properties) { 

and items were not previously defined, then items will be a global variable. Since JSHint is complaining, you probably won't declare items in the scope of the for constructor, even if you let it look like your code example.

If this is really defined, I would suggest an error message file with JSHint :-)

+3
source

This seems like a bug in JSHint: https://github.com/jshint/jshint/issues/1016

+3
source

The correct answer is also documented everywhere.

 for (items in properties) { } 

it should be:

 for (var items in properties) { } 

(I know this is necropolisation)

+3
source

Source: https://habr.com/ru/post/1480665/


All Articles