JSFiddle - React, Tailwind, and code Playground
by Liu David
HTML
<script src="https://cdn.bootcss.com/jquery/3.1.0/jquery.min.js"></script>
<button id="btnTest">
测试
</button>
<button id="btnTest2">
测试局部变量的生存期
</button>
JavaScript
function test(arr) {
var myArr = arr.map(function (obj) {
return 'great ' + obj;
});
$('#btnTest').on('click', function () {
alert(myArr[1]);
});
//following change will change the alert
//so js variable's scope is in function?
//myArr = ['hello', 'js'];
}
var myArr = ['hello', 'world'];
test(myArr);
myArr = ['hello', 'js'];
myArr = null;
//+++++++++++++++++++
function test2(){
var i=0;
$('#btnTest2').on('click', function () {
alert(i);
i++;
});
}
test2();