var test = (function () { var name = 'lyl' var age = '18' return{ getname: function (){ return name }, getage: function(){ return age } } })(); console.log(test.getname()) console.log(test.getage())
var test = (function () { var name = 'lyl' var age = 18 function myadd (){ age=age+1 } return{ getname: function (){ return name }, getage: function(){ return age }, addage: function(){ myadd() } } })(); console.log(test.getname()) console.log(test.getage()) test.addage() test.addage() test.addage() test.addage() console.log(test.getage())
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Closures