「PythonとJavaScriptではじめるデータビジュアライゼーション」を読む

2.4.8 JavaScriptクロージャとモジュールパターン

2-4-8.js
function Counter(inc){
    var count = 0;
    var add = function(){
        count += inc;
        console.log('Current count:' + count);
    }
    return add;
}

var inc2 = Counter(2);
inc2();
inc2();
inc2();
inc2();

index.html
underscoreライブラリ関係ないがいれておく
<!-- index.html -->
<!DOCTYPE html>
<meta charset="utf-8">
<div id='viz'></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script type="text/javascript" src="2-4-8.js" async></script>

$python -m http.server

WEBブラウザのアドレス欄にhttp://localhost:8000を入力して
Ctrl+shift+IでDevToolsのコンソールタグを表示させる   

f:id:bitop:20170903133756p:plain