0

I'm reading through JavaScript Succinctly (a free ebook from Syncfusion), and there's a section entitled "Redefining function parameters" which I don't quite understand. Wondering if someone could explain what's going on.

var foo = false;
var bar = false;

var myFunction = function(foo, bar) {
  arguments[0] = true;
  bar = true;
  console.log(arguments[0], bar);
}

myFunction();

I was under the impression that function parameters, whatever they're called, are locally scoped variables -- even if the parameter name is also the name of a globally scoped variable. So, the above code, rather than 'redefining' the function parameters is just defining the parameters. Since, if, after the function is called, if you log bar to the console, it logs false. So, yeah, I'm a little confused here. Is the book wrong or am I confused about parameters' names and variables?

  • Possible duplicate of [What is the scope of variables in JavaScript?](http://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript) – Hacketo Feb 26 '16 at 13:46
  • Add a console line before: `var myFunction = function(foo, bar) { console.log(foo,bar); arguments[0] = true; bar = true; console.log(arguments[0], bar); }` And what do you see? – epascarello Feb 26 '16 at 13:47
  • I don't understand your question. You say *is the book wrong*, yet you do not tell us what the book says. –  Feb 26 '16 at 14:04
  • @epascarello Prior to the assignments they log `undefined`. But doesn't that mean that it's not an instance of *re*defining. Or am I missing something? @torazaburo Sorry, I thought it was implicit in the framing of the question. The book's section is called "Redefining function parameters" -- That's *re*-defining. My confusion is as to whether there's reason to call it *re*defining or if it's in fact just *defining*. I realize this might be an utterly pedantic, purely semantic question. But if it's not, I want to know why, you know? –  Feb 26 '16 at 14:11
  • The arguments have NOTHING to do with the two variables defined outside of the function. Only thing in common is someone named them the same thing to make it confusing. You do not pass any arguments when you call the method so they are undefined. – epascarello Feb 26 '16 at 14:15

2 Answers2

-1

Define outer foo as false (this variable is never touched after this line)

var foo = false;

Define outer bar as false; (this variable is never touched after this line)

var bar = false;

Define myFunction and give it a function as a value.

That function defines inner foo and inner bar which get values when the function is called.

var myFunction = function(foo, bar) {

Redefine whatever the value of inner foo is to be true

  arguments[0] = true;

Redefine whatever the value of inner bar is to be true

  bar = true;

Log the values of inner foo and inner bar

  console.log(arguments[0], bar);
}

Call myFunction with no arguments, which defines inner foo and inner bar as undefined.

myFunction();
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • You say *Redefine whatever the value of inner `foo` is to be true*, but in strict mode, `arguments[0]` does not track the value of `foo`, nor does `foo` track the value in the corresponding `arguments[0]`. –  Feb 26 '16 at 14:03
  • 1
    @torazaburo — The question isn't asking about code that uses strict mode though. – Quentin Feb 26 '16 at 14:03
  • Fine, let's split hairs. the question also did not specify that the code was non-module code. But all module code is strict code. So if I just write a module, the behavior you describe is incorrect. –  Feb 26 '16 at 14:11
-1

I'm reading through JavaScript Succinctly (a free ebook from Syncfusion)

Please take everything you read there with a grain of salt. That book discusses ECMAScript 3, which is severely outdated. Many things are still the same (the web needs backwards compatibility), but some have changed.

As an example, reassigning to function parameters using the arguments objects is no more possible in ES5 strict mode.

I was under the impression that function parameters, whatever they're called, are locally scoped variables

Yes, that's true.

even if the parameter name is also the name of a globally scoped variable.

Yes, this is called shadowing. Why there are global variables in that examples evades my grasp.

So, the above code, rather than 'redefining' the function parameters is just defining the parameters

Yes. The parameters do define the variables, and the assignment statements in the body reassign them (from undefined to true).

Bergi
  • 630,263
  • 148
  • 957
  • 1,375