0

I came across this excerpt while reading Chapter 2 of "You Don't Know JS Yet".

But beware, it's more complicated than you'll assume. For example, how might you determine if two function references are "structurally equivalent"? Even stringifying to compare their source code text wouldn't take into account things like closure.

I just want to make sure if I understand correctly on what the author meant by "closure". I'm thinking of this example:

function x() {
  console.log('Hello');
}

const foo = x;

function y() {
  const bar = x;
  if(foo.toString() === bar.toString()) { // returns true but the closure of foo and bar is different 
    // do something
  }
}

Also, under what circumstances we need to compare two functions? Thanks.

Janice Zhong
  • 836
  • 7
  • 16
  • 1
    "*returns true but the closure of foo and bar is different*" no, in this case, you're comparing literally the same function. `foo === bar` is `true`. It can't be different. – VLAZ Feb 04 '23 at 10:59
  • "*what the author meant by "closure*": [What is a 'Closure'?](https://stackoverflow.com/q/36636) | [What is the exact definition of a closure?](https://stackoverflow.com/q/1095707) | [what is the formal defenition of closure? and does it depend on the programming languege?](https://stackoverflow.com/q/55922656) | [What exactly does "closure" refer to in JavaScript?](https://stackoverflow.com/q/1801957) | [What does it mean to "close over" something?](https://stackoverflow.com/q/30700027) | [How do JavaScript closures work?](https://stackoverflow.com/q/111102) – VLAZ Feb 04 '23 at 11:02

3 Answers3

2

Here is an example of two functions that look the same yet will behave differently because of their closure:

const what="great",
      fun1=()=>console.log(`This is ${what}!`);

{ // this block will have its own closure
 const what="stupid",
       fun2=()=>console.log(`This is ${what}!`);

 console.log(fun1.toString()==fun2.toString());
 fun1();
 fun2();
}
Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43
1

The author means that closure is the data that a function carries with it, including the variables from its surrounding scope that are used in its body. In the example you gave, even though the two functions foo and bar have the same source code (as indicated by the toString() comparison), they are not structurally equivalent because they have different closure values.

As for when to compare two functions, you might need to compare two functions in certain scenarios, such as when you want to determine if two functions have the same behavior, if they have the same closure, or if they are bound to the same execution context.

0

As far as I can tell, the author is saying "closure" as a reference to scope. I think you are correct.

As for the comparison of two functions, the most obvious case that comes to mind is comparing the time and space complexity performance of two functions that perform the same overall task. However, in regards to the authors 'structurally equivalent' notion, I don't know.