var add = (function () {
var counter = 0;
return function () {return counter += 1;}
})();
add();
add();
add();
加深理解
<html>
<head>
</head>
<body>
</body>
<script>
var a = 3;
debugger;
console.log(a)
var a = 4;
console.log(a);
(function add(){
console.log(a)
a = a+1
console.log(a)
})()
console.log(a);
function redefine(){
console.log(a)
var a = 9
console.log(a)
}
redefine()
console.log(a)
a = 50
console.log(a)
console.log(this.a)
console.log(window.a)
</script>
</html>
<html>
<head>
</head>
<body>
</body>
<script>
var counter = 1;
var add = (function(){
var counter = 5;
return function(){
counter+=1
console.log(counter)
}
})();
console.log(counter)
add()
add()
add()
console.log(counter)
</script>
</html>
|