Skip to content

Commit f40f16d

Browse files
committed
Ejemplos de funciones y objetos
1 parent 89bc48b commit f40f16d

File tree

9 files changed

+154
-0
lines changed

9 files changed

+154
-0
lines changed

2023-03-07/args.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
function showParams2(a, b, c) {
3+
console.log(`${a} ${b} ${c}`);
4+
}
5+
6+
showParams2();
7+
showParams2(1);
8+
showParams2(1, 2);
9+
showParams2(1, 2, 3);
10+
showParams2(1, 2, 3, 4);
11+
showParams2(1, 2, 3, 4, 5);
12+
showParams2(1, 2, 3, 4, 5, 6);

2023-03-07/argumentos.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
function showParams(a, b, c) {
3+
console.log(`${a} ${b} ${c}`);
4+
}
5+
6+
showParams();
7+
showParams(1);
8+
showParams(1, 2);
9+
showParams(1, 2, 3);
10+
showParams(1, 2, 3, 4);
11+
showParams(1, 2, 3, 4, 5);
12+
showParams(1, 2, 3, 4, 5, 6);

2023-03-07/by_value_by_ref.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
function cambia_nombre(person) {
3+
person.name = "Pepito";
4+
}
5+
6+
const agente = {
7+
name: "James",
8+
lastname: "Bond",
9+
}
10+
11+
cambia_nombre(agente);
12+
console.log(agente);

2023-03-07/copy_object.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function copy_object(orig) {
2+
let result = {};
3+
for (const field in orig) {
4+
result[field] = orig[field];
5+
}
6+
return result;
7+
}
8+
9+
let a = { uno: 1, dos: 2, tres: 3, cuatro: { x: 0, y: 0 } };
10+
11+
let b = copy_object(a);
12+
console.log(a === b);
13+
console.log(b);
14+
console.log(a.cuatro === b.cuatro);
15+
16+
let c = structuredClone(a);
17+
console.log(a === c);
18+
console.log(c);
19+
console.log(a.cuatro === c.cuatro);
20+

2023-03-07/functions-typed.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
type Palabra = string;
3+
4+
type ExclamateFunction = (s: string, numexcl?: number) => string;
5+
6+
let exclama_t : ExclamateFunction = function (str, numExcl = 1) {
7+
let result = str + "!".repeat(numExcl);
8+
return result;
9+
}
10+
11+
function max2_t(a: number, b: number): number {
12+
if (a > b) {
13+
return a;
14+
}
15+
return b;
16+
}
17+
18+
console.log(exclama_t("hola", 7));
19+
console.log(exclama_t("adios", 1));
20+
console.log(exclama_t("To be or not to be"));
21+
console.log(exclama_t("pardiez"));

2023-03-07/functions.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
function exclama(str, numExcl = 1) {
3+
let result = str + "!".repeat(numExcl);
4+
return result;
5+
str = "";
6+
numExcl = 3;
7+
}
8+
9+
function max2(a, b) {
10+
if (a > b) {
11+
return a;
12+
}
13+
return b;
14+
}
15+
16+
console.log(exclama("hola", 7));
17+
console.log(exclama("adios", 1));
18+
console.log(exclama("To be or not to be"));
19+
console.log(exclama("pardiez"));

2023-03-07/hitler.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function sayHi(person) {
2+
if (person === 'Adolf Hitler') {
3+
return;
4+
}
5+
console.log('Oh hi ${person}!');
6+
}
7+
8+
console.log(sayHi("Adolf Hitler"));

2023-03-07/objects.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
// Map<string, any>
3+
4+
let obj = {
5+
a: 1,
6+
b: false,
7+
c: "hola",
8+
subobj: {
9+
d: null,
10+
e: undefined
11+
}
12+
};
13+
14+
console.log('c' in obj);
15+
16+
console.log(obj);
17+
18+
// Añadir campos
19+
console.log(obj.g);
20+
obj.f = "aquí he estao yo";
21+
obj["g"] = "pues yo más";
22+
obj["hola que tal"] = 33;
23+
24+
// Borrar campos
25+
delete obj.c;
26+
27+
// Acceder a campos con un un string
28+
let campo = "f";
29+
obj[campo] = "Yo también";
30+
let estaCampo = campo in obj; // true
31+
32+
console.log(obj);
33+
34+
let obj2 = {
35+
name: "___",
36+
["a b c"]: 237,
37+
"*popy*": null,
38+
"abc": 890,
39+
};
40+
obj2.abc = 891;
41+
console.log(obj2);
42+

2023-03-07/useless.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
function useless(a, b) {
3+
a++;
4+
b = b;
5+
}
6+
7+
let result = useless(8, 3);
8+
console.log(result);

0 commit comments

Comments
 (0)