Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Javascript strict mode can you assign arguments to another var?

I'm trying to validate Javascript code through Sonar and I have some doubts about strict mode rules.

The rule I'm validating is about "eval" and "arguments" behavior in strict mode.

In reading this rule description, it seems I can not assign arguments to new variable for accessing it later or in a loop.

On reading some documentation, from strict mode on Firefox and strict mode on IE the rules I found basically are (that apply to this case):

  1. I can't have any variables/functions/etc named arguments since this is a reserved word.
  2. arguments can't be bound

But, the sample code above seems to be violating that Sonar rule:

var args = arguments;

So, is this a false-positive or is this piece of code violating strict mode?

like image 818
lucasarruda Avatar asked Nov 21 '13 17:11

lucasarruda


People also ask

Is VAR allowed in strict mode?

Not Allowed in Strict Mode Objects are variables too. Deleting a variable (or object) is not allowed.

What happens when you invoke strict mode in JavaScript?

Strict mode changes some previously-accepted mistakes into errors. JavaScript was designed to be easy for novice developers, and sometimes it gives operations which should be errors non-error semantics. Sometimes this fixes the immediate problem, but sometimes this creates worse problems in the future.

What is the correct way to run a JavaScript in strict mode?

You can enable the strict mode by declaring this in the top of your script/function. 'use strict'; When a JavaScript engine sees this directive, it will start to interpret the code in a special mode.

Which is not a result of invoking strict mode in JavaScript?

Strict mode will throw reference error when found non declared variables and in some cases. If you have such an unrestrictedly typed code, that is used variables without declaring.


1 Answers

Based on the users answers and some researches, I've found this code (var args = arguments;) to be valid, since it's not modifying arguments, but only assigning it to another variable.

Please note that, since args now points to arguments it can't be modified also. But it can be read normally.

Update: Fix will be available under new release Javascript plugin (1.5).

like image 67
lucasarruda Avatar answered Oct 26 '22 16:10

lucasarruda