-1

I recently interviewed a candidate for an entry level web developer position and asked this question: What might you call the following line of code in Javascript:

let foo = function() {}

The question was purposefully vague because the resume was weak and I wanted to get a sense of how the candidate worked through all of the possible answers to the question. After a few moments the response was essentially "I don't know, what's the right answer?"

I explained that there are a few answers: Variable declaration, function assignment, hoisting mitigation, ES6, etc. Then I said it could also be considered a closure. To that, the candidate responded, "I don't think that's a closure. A closure is a function inside of function."

It's the first time I've ever had a candidate challenge my response to a question to which they didn't know the answer and it made me wonder if a closure could be boiled down to a simple one liner. Am I wrong about that?

Edit: This is only for my own learning and has nothing to do with the candidate. In fact, we extended an offer and they accepted. They start next week :)

divups
  • 114
  • 9
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/200490/discussion-on-question-by-jeffrey-barrett-is-this-really-considered-a-javascript). – deceze Oct 07 '19 at 04:46

4 Answers4

1

https://en.wikipedia.org/wiki/Closure_(computer_programming)#Anonymous_functions

The term closure is often used as a synonym for anonymous function, though strictly, an anonymous function is a function literal without a name, while a closure is an instance of a function, a value, whose non-local variables have been bound either to values or to storage locations (depending on the language; see the lexical environment section below).

it's an anonymous function assigned to a block scoped variable, I guess it could be considered to be a closure, if it's in a block.

Jasen
  • 11,837
  • 2
  • 30
  • 48
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/200491/discussion-on-answer-by-jasen-is-this-really-considered-a-javascript-closure). – deceze Oct 07 '19 at 04:47
  • Please be more context sensitive on what is moved. Just because a tail-end turns into a “discussion” (fsvo), does not imply earlier comments (especially those that clarify answers) are “discussions”. – user2864740 Oct 07 '19 at 06:18
1

I think we can agree that a function doesn't have to be defines inside another function to be a closure. I'd probably have followed up and asked why they think that that's a requirement for being a closure and then direct the conversation towards scope in general.

People may have different opinions about what really constitutes a closure, but I like the description from Wikipedia (which used to be a bit more extensive) which I have been using whenever I teach JavaScript:

Operationally, a closure is a record storing a function together with an environment.

I like it because it is a very easy to understand description, and it applies easily to JavaScript functions, because that's exactly how it works as described in the spec. The spec even uses the term closure to refer to a created function, irrespectively of the actual content of the function.

And following that thought, every function in JavaScript is a closure, so you are right.

Having said that, I could see someone making the argument that this function is not used as a closure, since it doesn't access any free variables.


I also have to add that "closure" wouldn't be the first thing that comes to my mind when looking at this code. It's easier to name the things that are there vs that are not there (referring to the function not be used as a closure).

The first thing that would come to my mind is function expression.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
0

If you want to consider that any type of function can be considered a closure, what is the point of using a homonymous term to talk about functions; and especially how to distinguish this specific type of coding done to protect its contents?

PO question: "the most minimal valid closure" that I can do for the moment

answer for: MisterJojo OK thanks, can you give me an example of the most minimal valid closure? – Jeffrey Barrett

// prepared  logic for specific closures functions
const fCount=_=>{let x=0;return _=>x++}

// closures functions :
let myCount = fCount()
let yourCount = fCount()

// proofs of ... 
console.log( 'my', myCount() )  // 0
console.log( 'my', myCount() )  // 1
console.log( 'my', myCount() )  // 2

console.log( 'your', yourCount() )  // 0
console.log( 'your', yourCount() )  // 1
console.log( 'your', yourCount() )  // 2
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {min-height:100% !important; top:0;}

fCount is "closed" => there is no access to his 'x'.

this is why we call this kind of function code a "closure" (in JS there is no private variables)

Mister Jojo
  • 20,093
  • 6
  • 21
  • 40
0

It is not a closure - it is merely an ‘anonymous’ function. However, the reason given is incorrect.

It is not a closure because it does not establish a lexical binding to a variable in an outer scope. The variable binding itself need not be mutable - although most closures (in JavaScript) employ variable reassignment.

Here is a minimally valid closure. See the quote and link given in other answer, as such is merely an extraction of a comment:

let a = 1
let f = () => a

This is a closure because a lexical binding is established. No secondary function is required. EOD/QED.

The variable (a) captured in the closure could refer to a mutable object. It could also be trivially written in a loop with the let having (loop) block scope. At no time is is there a requirement that the captured variable is itself reassignable (or even mutable): refer to Haskell and Java closures.

Here is a good example of closures in a loop that employ “let”: https://wesbos.com/for-of-es6/ Note that a secondary function is not required to establish a closure (which is a nice deviation from earlier ECMAScript versions).

user2864740
  • 60,010
  • 15
  • 145
  • 220
  • in this case everything is a closure, so why need to call this a closure and not a variable or a function? by this way, this message itself is a closure – Mister Jojo Oct 07 '19 at 04:45
  • Only functions that CAPTURE lexical scope bindings are closures (see the first sentence). An empty function, trivially, does not capture a binding (at least insofar as exposed ‘directly’) and is thus not a closure. – user2864740 Oct 07 '19 at 06:15
  • You make a mistake on the function concerned, I added comments on my post, to specify them – Mister Jojo Oct 07 '19 at 12:43