Created
October 31, 2015 01:11
-
-
Save jokester/8d204339f8163abe0e11 to your computer and use it in GitHub Desktop.
只能由特定code使用的, 只定義一次的變數
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.getElementById('testbutton').onclick = (function () { | |
// 這邊的t和a只會定義一次 | |
var t = Math.tan(Math.PI * 0.2); | |
var a; | |
// 下面的function每次實行時都會重復使用上面的t和a | |
// 其他code看不到上面的t和a | |
return function() { | |
var i = 0; | |
for(; i < 1000; i ++){ | |
a = i / t; | |
//do something | |
} | |
}; | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
var t = Math.tan(Math.PI * 0.2); | |
var a; | |
document.getElementById('testbutton').onclick = function () { | |
var i = 0; | |
for(; i < 1000; i ++){ | |
a = i / t; | |
//do something | |
}; | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment