¿Qué otras opciones hay para pasar y usar argumentos en una función usando un objeto además de estos dos?
Opción 1:
let timerClosure = function ({ period, // number funStart, // function funEnd, // function funStartArguments = [], funEndArguments = [], warming = 0 }) {// something }Opcion 2:
let timerClosure = function (timerConfigObj) { let period = timerConfigObj.period; // number let funStart = timerConfigObj.funStart; let funEnd = timerConfigObj.funEnd; let funStartArguments = timerConfigObj.funStartArguments || []; let funEndArguments = timerConfigObj.funStartArguments || []; let warming = timerConfigObj.warming || 0; }Esas, u otras formas de hacerlos girar, son básicamente eso. Bueno, eso y usar una matriz, pero si usa una matriz, también puede usar parámetros discretos, tendrá el mismo problema con el orden significativo y los problemas con eso a medida que llega a más de tres parámetros.
Otra forma de girar lo que tienes:
let timerClosure = function (timerConfigObj) { const { period, // number funStart, // function funEnd, // function funStartArguments = [], funEndArguments = [], warming = 0 } = timerConfigObj; // ... };Has dicho "... y describiendo" en el título pero no en el texto. Si esa parte es importante para usted, puede describirla de manera más completa mediante el uso de anotaciones JDDoc , que muchos IDE pueden leer y presentarle (incluso si nunca ejecuta JSDoc) cuando usa la función:
/** * Does something nifty with timers and closures. * * @param {Object} options - Options for the nifty thing. * @param {number} options.period - `period` description... * @param {function} options.funStart - `funStart` description... * @param {function} options.funEnd - `funEnd` description... * @param {array} options.funStartArguments - `funStartArguments` description... * @param {array} options.funEndArguments - `funEndArguments` description... * @param {number} options.warning - `warning` description... */ let timerClosure = function ({ period, // number funStart, // function funEnd, // function funStartArguments = [], funEndArguments = [], warming = 0 }) { // ... };Del mismo modo, si crea un tipo/interfaz de TypeScript y documenta sus propiedades, los IDE también se lo mostrarán.
/** * Options for `timerClosure` */ interface TimerClosureOptions { /** * Period description... */ period: number; funStart: function; funEnd: function; funStartArguments?: any[]; funEndArguments?: any[]; warming?: number; } /** * Does something nifty with timers and closures. * * @param {TimerClosureOptions} options - Options for the nifty thing. */ let timerClosure = function ({ period, funStart, funEnd, funStartArguments = [], funEndArguments = [], warming = 0 }: TimerClosureOptions) { // ... };