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

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

programmeradmin1浏览0评论

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?

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?

Share Improve this question asked Nov 21, 2013 at 17:33 lucasarrudalucasarruda 1,4821 gold badge26 silver badges45 bronze badges 16
  • Why do you think this is validating the rule? – dbarnes Commented Nov 21, 2013 at 17:38
  • 1 I think it's not violating strict mode, you can have it. – Mahdi Commented Nov 21, 2013 at 17:40
  • 1 It doesn't violate, you can check it yourself in console in developer tools. – vortexwolf Commented Nov 21, 2013 at 17:43
  • 1 var args = arguments binds args, not arguments. The Sonar page you linked forbids binding arguments, and it seems to agree with the ECMAScript5 spec. (ecmascript/docs/tc39-2009-043.pdf, page 233) – psquared Commented Nov 21, 2013 at 17:49
  • 1 I wouldn't be surprised if it's just being safe and not checking to see if args is used anywhere. I bet they made the assumption that args will be altered so you can't assign arguments to anything. – dbarnes Commented Nov 21, 2013 at 18:21
 |  Show 11 more ments

1 Answer 1

Reset to default 6

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).

发布评论

评论列表(0)

  1. 暂无评论