Skip to main content
added 319 characters in body
Source Link
Benjamin Gruenbaum
  • 274.6k
  • 89
  • 514
  • 511

This is because the book assumes you're using strict mode.

If you run the code in strict mode, you'll get the behavior you'd expect given what's written in the book.

function doAdd(num1 , num2) {
     "use strict";
     arguments[1] = 10;
     num2 = 40;
     alert(arguments[0] + arguments[1]);
}
doAdd(10 , 20); // this alerts 20, alerts 50 in nonstrict mode

A little known fact is that the entire point of strict mode is to avoid this sort of dynamic scoping (which is why arguments is fixated, with isn't allowed and eval behaves differently). Aside from clarity gains, this allows for massive speed improvements.

Also see this:

For strict mode functions, the values of the arguments object‘s properties are simply a copy of the arguments passed to the function and there is no dynamic linkage between the property values and the formal parameter values.

This is what NCZ means in context, quoting the book itself:

Strict mode makes several changes to how the arguments object can be used. First, assignment, as in the previous example, no longer works. The value of num2 remains undefined even though arguments[1] has been assigned to 10. Second, trying to overwrite the value of arguments is a syntax error. (The code will not execute.) - Page 82, Language Basics, Professional JavaScript, Nicholas C. Zakas

This is because the book assumes you're using strict mode.

If you run the code in strict mode, you'll get the behavior you'd expect given what's written in the book.

function doAdd(num1 , num2) {
     "use strict";
     arguments[1] = 10;
     num2 = 40;
     alert(arguments[0] + arguments[1]);
}
doAdd(10 , 20); // this alerts 20, alerts 50 in nonstrict mode

A little known fact is that the entire point of strict mode is to avoid this sort of dynamic scoping (which is why arguments is fixated, with isn't allowed and eval behaves differently). Aside from clarity gains, this allows for massive speed improvements.

This is because the book assumes you're using strict mode.

If you run the code in strict mode, you'll get the behavior you'd expect given what's written in the book.

function doAdd(num1 , num2) {
     "use strict";
     arguments[1] = 10;
     num2 = 40;
     alert(arguments[0] + arguments[1]);
}
doAdd(10 , 20); // this alerts 20, alerts 50 in nonstrict mode

A little known fact is that the entire point of strict mode is to avoid this sort of dynamic scoping (which is why arguments is fixated, with isn't allowed and eval behaves differently). Aside from clarity gains, this allows for massive speed improvements.

Also see this:

For strict mode functions, the values of the arguments object‘s properties are simply a copy of the arguments passed to the function and there is no dynamic linkage between the property values and the formal parameter values.

This is what NCZ means in context, quoting the book itself:

Strict mode makes several changes to how the arguments object can be used. First, assignment, as in the previous example, no longer works. The value of num2 remains undefined even though arguments[1] has been assigned to 10. Second, trying to overwrite the value of arguments is a syntax error. (The code will not execute.) - Page 82, Language Basics, Professional JavaScript, Nicholas C. Zakas

Source Link
Benjamin Gruenbaum
  • 274.6k
  • 89
  • 514
  • 511

This is because the book assumes you're using strict mode.

If you run the code in strict mode, you'll get the behavior you'd expect given what's written in the book.

function doAdd(num1 , num2) {
     "use strict";
     arguments[1] = 10;
     num2 = 40;
     alert(arguments[0] + arguments[1]);
}
doAdd(10 , 20); // this alerts 20, alerts 50 in nonstrict mode

A little known fact is that the entire point of strict mode is to avoid this sort of dynamic scoping (which is why arguments is fixated, with isn't allowed and eval behaves differently). Aside from clarity gains, this allows for massive speed improvements.