0

This is a nonsensical example used purely for illustration purposes:

function a() {
  return b().bar + foo.bar;
}

function b() {
  var foo = {
    bar: 'baz'
  };
  return foo;
}

console.log(a()); // Obviously throws a Uncaught ReferenceError: foo is not defined.

What I would like to understand is:

1) Is foo resolved as an anonymous object (Object {bar: "baz"}) in function b before returning? Or is it resolved in function a after foo is returned?

2) Since the return value of the b() call is cached temporarily in order to execute the string concatenation, would it be possible to access that anonymous object during run time, like I tried? Well, it's obviously not called "foo" anymore, but it's temporarily in the scope of function a, so the function scope knows of it's location, right?

For example:

function a() {
  return b().bar + b().bar;
}

function b() {
  var foo = {
    bar: 'baz'
  };
  return foo;
}

console.log(a()); // logs "bazbaz".

Which means the result of the first b() call is stored somewhere on the stack until the second one returns. Is it possible to refer to this object directly during run-time?

Anon
  • 109
  • 1
  • 8
  • 3
    The name foo only exists inside your the scope of `function b`. The name is meaningless outside of that function. – Will Reese Jun 14 '17 at 07:32
  • 1
    All objects are "anonymous" in JavaScript. There is no general concept of an object having a name. – T.J. Crowder Jun 14 '17 at 07:35
  • 1
    _Is it possible to refer to this object directly during run-time?_ `var foo2 = b();` as far as I'm aware it'll be pointing to the same object as the object that's created in the `b` function, objects are passed by reference in JS. – George Jun 14 '17 at 07:38
  • 1
    @George: Be careful there, "pass by reference" is a term of art referring **specifically** to passing references to *variables* into functions. JavaScript doesn't have pass by reference at all. The *value* passed into and out of functions related to objects is called an object reference, but that's a completely different use of the word "reference." – T.J. Crowder Jun 14 '17 at 07:39
  • @T.J.Crowder What's the difference if you don't mind explaining? Other than one is a decision to pass by reference made by the programmer and the other is just how the JS engine handles objects. – George Jun 14 '17 at 07:44
  • 1
    @George: Different functionality. See [this answer](https://stackoverflow.com/a/13952217/157247), which is for Java but JavaScript is the same in this regard. – T.J. Crowder Jun 14 '17 at 08:09
  • 1
    @George https://stackoverflow.com/questions/16880418/javascript-pass-object-as-reference is also quite relevant and illustrates the difference pretty well. – Anon Jun 14 '17 at 08:15
  • @T.J.Crowder Thank you, I think I get it now, JS object reference is more of a copy of the reference rather than directly passing the reference, so although _similar_ it is different. I'll have to read more on it rather than asking questions here and polluting the comments :) – George Jun 14 '17 at 08:34

1 Answers1

4

1) Is foo resolved as an anonymous object (Object {bar: "baz"}) in function b before returning? Or is it resolved in function a after foo is returned?

The identifier foo exists only within b. When you do return foo, foo's value is resolved and then set as the return value of b (whereupon that value no longer has any connection back to the identifier).

2) Since the return value of the b() call is cached temporarily in order to execute the string concatenation, would it be possible to access that anonymous object during run time, like I tried?

Not directly, you'd have to store that value somewhere in order to reuse it:

function a() {
  var bval = b();
  return bval.bar + bval.bar;
}

Which means the result of the first b() call is stored somewhere on the stack until the second one returns. Is it possible to refer to this object directly during run-time?

No, you can't directly access the stack.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875