Skip to content

Commit a0abb2e

Browse files
author
Ariel Porporatto
committed
Add other post es_ES
1 parent 32e2db9 commit a0abb2e

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
layout: post
3+
4+
title: Cómo utilizar argumentos opcionales en funciones (con callback opcional)
5+
tip-number: 54
6+
tip-username: alphashuro
7+
tip-username-profile: https://github.com/alphashuro
8+
tip-tldr: Usted puede hacer argumentos de función y callback opcional
9+
10+
categories:
11+
- es_ES
12+
---
13+
14+
Ejemplo de función donde los argumentos 2 y 3 son opcionales
15+
16+
```javascript
17+
function example( err, optionalA, optionalB, callback ) {
18+
// recuperar los argumentos como array
19+
var args = new Array(arguments.length);
20+
for(var i = 0; i < args.length; ++i) {
21+
args[i] = arguments[i];
22+
};
23+
24+
// Primer argumento es el objeto de error
25+
// shift() elimina el primer elemento del
26+
// array y lo devuelve
27+
err = args.shift();
28+
29+
// Si el último argumento es una función entonces es la funcion callback.
30+
// pop() elimina el último elemento del array
31+
// y lo devuelve
32+
if (typeof args[args.length-1] === 'function') {
33+
callback = args.pop();
34+
}
35+
36+
// Si args aún mantiene elementos, estos son
37+
        // sus elementos opcionales que se podía
38+
        // recuperar uno por uno como este:
39+
if (args.length > 0) optionalA = args.shift(); else optionalA = null;
40+
if (args.length > 0) optionalB = args.shift(); else optionalB = null;
41+
42+
// continuar como de costumbre: comprobar si hay errores
43+
if (err) {
44+
return callback && callback(err);
45+
}
46+
47+
// para los propósitos de tutoría, ingrese los parámetros opcionales
48+
console.log('optionalA:', optionalA);
49+
console.log('optionalB:', optionalB);
50+
console.log('callback:', callback);
51+
52+
/* haz tus cosas */
53+
54+
}
55+
56+
// ES6 con código más corto
57+
function example(...args) {
58+
// primer argumento es el objeto de error
59+
const err = args.shift();
60+
// si el último argumento es una función entonces es la funcion callback.
61+
const callback = (typeof args[args.length-1] === 'function') ? args.pop() : null;
62+
63+
// args si todavía tiene elementos, estos son los elementos opcionales que se podía recuperar de uno en uno como este:
64+
const optionalA = (args.length > 0) ? args.shift() : null;
65+
const optionalB = (args.length > 0) ? args.shift() : null;
66+
// ... repetir para mas items
67+
68+
if (err && callback) return callback(err);
69+
70+
/* haz tus cosas */
71+
}
72+
73+
// invocar la función de ejemplo con y sin argumentos opcionales
74+
75+
example(null, 'AA');
76+
77+
example(null, function (err) { /* do something */ });
78+
79+
example(null, 'AA', function (err) {});
80+
81+
example(null, 'AAAA', 'BBBB', function (err) {});
82+
```
83+
84+
### ¿Cómo se determina si optionalA o optionalB se destina?
85+
86+
El diseño de su función para requerir optionalA con el fin de aceptar optionalB

0 commit comments

Comments
 (0)