-1

I'm learning about Closures here. For some reason, I can't execute any of the examples. I get an Invalid or unexpected tokenerror, usually on the last lines of whatever block of code I execute. I have tried using Chrome's and Firefox's consoles, node.js and the integrated node.js environment in Visual Studio's Code editor. I have tried taking out comments, trimming spaces, removing indentation and no matter what I do I still get the same error. A friend of mine was able to execute the code without doing anything to it. This is an example:

function celebrityID () {
    var celebrityID = 999;
    // We are returning an object with some inner functions​
    // All the inner functions have access to the outer function's variables​
    return {
        getID: function ()  {
            // This inner function will return the UPDATED celebrityID variable​
            // It will return the current value of celebrityID, even after the changeTheID function changes it​
          return celebrityID;
        },
        setID: function (theNewID)  {
            // This inner function will change the outer function's variable anytime​
            celebrityID = theNewID;
        }
    }
​
}
​
​var mjID = celebrityID (); // At this juncture, the celebrityID outer function has returned.​
mjID.getID(); // 999​
mjID.setID(567); // Changes the outer function's variable​
mjID.getID(); // 567: It returns the updated celebrityId variable
     

This is driving me crazy. Please somebody help me solve this mystery.

jgozal
  • 1,480
  • 6
  • 22
  • 43

1 Answers1

1

There are invisible characters in you code which are throwing this errors, I've removed them here:

function celebrityID () {
    var celebrityID = 999;
    // We are returning an object with some inner functions​
    // All the inner functions have access to the outer function's variables​
    return {
        getID: function ()  {
            // This inner function will return the UPDATED celebrityID variable​
            // It will return the current value of celebrityID, even after the changeTheID function changes it​
          return celebrityID;
        },
        setID: function (theNewID)  {
            // This inner function will change the outer function's variable anytime​
            celebrityID = theNewID;
        }
    }
}

var mjID = celebrityID (); // At this juncture, the celebrityID outer function has returned.​
mjID.getID(); // 999​
mjID.setID(567); // Changes the outer function's variable​
mjID.getID(); // 567: It returns the updated celebrityId variable
    
  • This works! How did you remove the characters? what were they? – jgozal Jun 26 '16 at 17:48
  • I ran this code in the Google Chrome console, and I clicked on VM:16 to see in what line the error was, then I saw the red circles which are invisible characters –  Jun 26 '16 at 17:51
  • I didn't know those red circles were the actual characters. That's such a pain. Is there any easy way to remove them or avoid copying them when copying the examples? – jgozal Jun 26 '16 at 17:55
  • thank you. This works. Still a little annoying that these characters exist in all examples. – jgozal Jun 26 '16 at 18:12