var a =0;functionmyFunction(){
a++;
document.getElementById("demo").innerHTML = a;}myFunction()
再举个例子
//闭包应用 - 计算打车价格//打车起步13(3公里内),之后每多一公里增加五块钱,用户输入公里数可以计算打车价格//如果有拥堵情况:总价格多收取 10块钱var car =(function(){var start =13;//起步价 var total =0;//总价return{
price:function(kilo){if(kilo<=3){
total = start;}else{
total = start +(kilo -3)*5;}return total;},//正常情况下
yd:function(){return flag ? total +10:total;}//拥堵情况下}})();
console.log(car.price(23));
console.log('拥堵情况'+car.price());