企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 闭包的变形 ```javascript /* function fun(){ var a = 10; var b = 20; return { getA: function(){ return a; }, getB:function(){ return b; } } } var fun = fun(); var a = fun.getA(); var b = fun.getB(); console.log(a,b); */ // 写法二 /* function fun(){ var a = 10; var b = 20; function getA(){ return a; } function getB(){ return b; } return { getA:getA, getB:getB } } var fun = fun(); var a = fun.getA(); var b = fun.getB(); console.log(a,b); */ // 自执行函数 ()(); var modal = ( function fun(){ var a = 10; var b = 20; function getA(){ return a; } function getB(){ return b; } return { getA:getA, getB:getB } } )(); var a = modal.getA(); var b = modal.getB(); console.log(a,b); ```