2016-06-26 6 views
-1

私はクローズについて学んでいますhere。何らかの理由で、私は例を実行できません。 Invalid or unexpected tokenというエラーが表示されます。通常、実行するコードブロックの最後の行に表示されます。私は、ChromeとFirefoxのコンソール、node.js、および統合されたnode.js環境をVisual Studioのコードエディタで使用しようとしました。私はコメントを取って、スペースをトリミングし、インデントを削除しようとしましたが、私が何をしても同じエラーが発生します。私の友人は何もせずにコードを実行することができました。これは例です:http://javascriptissexy.com/でコードを正しく実行できないのはなぜですか?

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
  

これは私を狂っています。誰かが私にこの謎を解決するのを手伝ってください。

+0

を? – jgozal

+0

私は関数定義だけでも実行できないので、間違いなく問題ありません。 – jgozal

答えて

1

このエラーを投げているあなたのコードでは目に見えない文字がありますが、私はここでそれらを削除しました: `VARのmjID =`後

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
  
+0

これは機能します!どのように文字を削除しましたか?あれは何だった? – jgozal

+0

Google Chromeのコンソールでこのコードを実行しましたが、VM :16をクリックしてエラーの行を確認してから、目に見えない文字である赤い円を見ました。 –

+0

赤い円は実際のもの文字。それはそんな痛みです。例をコピーするときにそれらを削除するか、コピーを避ける簡単な方法はありますか? – jgozal

関連する問題