I am trying to understand one of the properties of js closures that says : ref from Closures store references to the outer function’s variables
function myId()
{
var userID=999;
return
{
getId: function() {
return userID;
},
setId: function(newId) {
userID = newId;
}
};
}
var callInnerFun = myId();
console.log(callInnerFun.getId());
callInnerFun.setId(500);
console.log(callInnerFun.getId());
When i try running the above code in Node or even on the browser, i get the following error:
SyntaxError: function statement requires a name at getId: function() {
I have tried and failed to understand what am i really missing out on. Is it some syntactical error, or is it something to do with my text editor, sublime text, because, if i try running the exact same code as copied from the link given above, then things work.
Whereas in my code (above) the logic is still the same as the reference, just the indentation and the variable names have changed. Is it that changing these have broken my code ?