企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
* 立即执行函数可以写成箭头函数的形式。 ``` (() => { console.log('Welcome to the Internet.'); })(); ``` * 那些需要使用函数表达式的场合,尽量用箭头函数代替。因为这样更简洁,而且绑定了 this。 ``` // bad [1, 2, 3].map(function (x) { return x * x; }); // good [1, 2, 3].map((x) => { return x * x; }); // best [1, 2, 3].map(x => x * x); ``` * 箭头函数取代Function.prototype.bind,不应再用 self/_this/that 绑定 this。 ``` // bad const self = this; const boundMethod = function(...params) { return method.apply(self, params); } // acceptable const boundMethod = method.bind(this); // best const boundMethod = (...params) => method.apply(this, params); ``` * 简单的、单行的、不会复用的函数,建议采用箭头函数。如果函数体较为复杂,行数较多,还是应该采用传统的函数写法。 * 所有配置项都应该集中在一个对象,放在最后一个参数,布尔值不可以直接作为参数。 ``` // bad function divide(a, b, option = false ) { } // good function divide(a, b, { option = false } = {}) { } ``` * 使用默认值语法设置函数参数的默认值。 ``` // bad function handleThings(opts) { opts = opts || {}; } // good function handleThings(opts = {}) { // ... } ```