What's important here is that the returned object doesn't expose the vars candidateOneVoteCount & candidateTwoVoteCount directly - these are only accessible via the returned functions in that object. For example:
Similarly, you're not able to overwrite those variables by setting properties on the returned object:
constelection=createElection("password")console.log(election.getResults("password")// { candidateOne: 0, candidateTwo: 0 }election.candidateOneVoteCount=1000000election.candidateTwoVoteCount=2000000// Still no votes for either candidate...console.log(election.getResults("password")// { candidateOne: 0, candidateTwo: 0 }
Its an example of functional programming being able to represent OO programing (as in the SICP text book). Even if Javascript didn't support objects, using closures and currying can give you "objects" with pure functional constructs.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Actually with createElection() you've effectively created an object. You could access it like this:
const obj = createElection()
Hey hey, thanks for reading 🍻
createElection()
does indeed return an object. Which is why this line works to destructure the object into const values:What's important here is that the returned object doesn't expose the vars
candidateOneVoteCount
&candidateTwoVoteCount
directly - these are only accessible via the returned functions in that object. For example:Similarly, you're not able to overwrite those variables by setting properties on the returned object:
Its an example of functional programming being able to represent OO programing (as in the SICP text book). Even if Javascript didn't support objects, using closures and currying can give you "objects" with pure functional constructs.