私はクローズについて学んでいます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
これは私を狂っています。誰かが私にこの謎を解決するのを手伝ってください。
を? – jgozal
私は関数定義だけでも実行できないので、間違いなく問題ありません。 – jgozal