企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
[TOC] ## 仅仅初始化一次 bad ``` function whichSideOfTheForce1(name) { const light = ['Luke', 'Obi-Wan', 'Yoda']; const dark = ['Vader', 'Palpatine']; return light.includes(name) ? 'light' : dark.includes(name) ? 'dark' : 'unknown'; } whichSideOfTheForce1('Luke'); whichSideOfTheForce1('Vader'); ``` good ``` function whichSideOfTheForceClosure1(name) { const light = ['Luke', 'Obi-Wan', 'Yoda']; const dark = ['Vader', 'Palpatine']; console.log("run"); return (name) => (light.includes(name) ? 'light' : dark.includes(name) ? 'dark' : 'unknown'); } const whichSideOfTheForce2 = whichSideOfTheForceClosure1(); whichSideOfTheForce2('Luke'); whichSideOfTheForce2('Vader'); ```