-
(() => {
console.log('Welcome to the Internet. Please follow me.');
})();
7.5 永远不要把参数命名为
arguments
。这将取代原来函数作用域内的arguments
对象。// bad
function nope(name, options, arguments) {
// ...stuff...
}
function yup(name, options, args) {
// ...stuff...
}
7.7 直接给函数的参数指定默认值,不要使用一个变化的函数参数。
// really bad
function handleThings(opts) {
// 不!我们不应该改变函数参数。
// 更加糟糕: 如果参数 opts 是 false 的话,它就会被设定为一个对象。
// 但这样的写法会造成一些 Bugs。
//(译注:例如当 opts 被赋值为空字符串,opts 仍然会被下一行代码设定为一个空对象。)
// ...
// still bad
function handleThings(opts) {
if (opts === void 0) {
opts = {};
}
// ...
}
// good
function handleThings(opts = {}) {
}