Jslint claims variable already defined? (one)

function wrapper() { var $R = {}; $R.expandFont = function (direction, max_time) { // wtf? $R jslint error var self = this, el_prim = self[0], $R = {}; alert(direction + max_time + el_prim + $R); }; } 

This snippet gives an error:

 line 573 character 13 '$R' is already defined. 

I think it is clear that it has not yet been determined.

$ R has been defined externally, but this should not be relevant. I should be able to define a local variable with the same name as JavaScript (language) - this is the scope. Yes, I know that this is not a block area, but the function works.

These are the basic rules for coverage. What gives?

Is this a jslint error?

+4
source share
4 answers

This is a new feature in JSLint. It was added commit on July 24, 2013. The following example demonstrates the cause of the error:

 (function () { var a = 1; // This declaration... return function () { var a = 2; // ... is shadowed by this one. }; }()); 

It seems that the warning is only issued when JSLint detects a variable declared in the scope (and not in the global scope), which is then obscured (which may explain why commentators on your question could not reproduce it).

It seems that there is currently no way to disable this warning.

In Crockford, the following message about this new warning:

JSLint now warns when a var parameter is defined that has the same name as the outer scope. This is confusing because the reader cannot easily determine which variable he is looking at. Sometimes this is a mistake, because a new variable accidentally hides the old one. In some cases, one of the old ones is used.

I will get a page fully explaining this at http://jslinterrors.com as soon as I get the chance.

+6
source

I think that you yourself understand that if you rename $R in the external or internal area of ​​the function, the "JSLint" error will be fixed.

I decided to write my answer only because I think there is a misunderstanding regarding the purpose of JSLint. This is not just a tool to help you find bugs in terms of the JavaScript language. You can view the tool as AddOn in a JavaScript book : The Good Parts . Douglas Crockford tried to show which language constructs might be misunderstood by people who read the code . Some of the possible misunderstandings, which he announced as “warnings,” and others as “errors.” Some of the “warnings” or “errors” may be suppressed by comments such as /*jslint ... */ there is no other (for example, the var declaration inside the for-loop header). The choice, which a potential misunderstanding should be interpreted as a “warning”, and which as a “mistake”, is very subjective and represents only the personal meaning of Douglas Crockford.

I do not always agree with the recommendations of Douglas Crockford, but the warning ("error"): '$R' is already defined I personally consider it also critical because of the difficulties with reading such code. I would recommend you rename one of the $R variables. I want to emphasize once again that the purpose of such changes is not to fix the JavaScript error, but to improve the readability of your program for other people .

By the way, I would recommend using variables with all uppercase letters only at the top level ( $R looks like this, regardless of the first letter $ ). See the last sentence for a JSLint naming recommendation .

+2
source
 line 573 character 13 '$R' is already defined 

This is because $ R is already defined. Two options:

Change the variable name

Or change your code to this:

 var $R = {}; $R.expandFont = function (direction, max_time) { // wtf? $R jslint error var self = this, el_prim = self[0]; $R = {}; 

Also see this DC post on redefinition .

+1
source

It's obvious that

$R is confusing. Jslint syntax analyzer and SO members. Change it when you move from the outer region to the inner region.

From $R to something like $PR or something else.

There is no need to cause this confusion when you can simply change the name of a variable.

Crockford is not a fool. These are good recommendations if you decide to follow them.

0
source

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


All Articles