You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
psicoCat.sound="Hey girl what's your name, what's your name?... I forgot";
92
+
93
+
// Constructor functions also have a prototype property which points to an object whose properties are automatically inherited by all objects created with that constructor:
94
+
Cat.prototype.speak=function(){
95
+
log(this.sound);
96
+
};
97
+
98
+
psicoCat.speak();// "Hey girl what's your name, what's your name?.. I forgot"
99
+
100
+
// Closure
101
+
/* The ability to treat functions as values, combined with the fact that local bindings are recreated every time a function is called,
102
+
leads to interesting possibilities. */
103
+
104
+
constincrementer=(function(n: number){
105
+
letlocal=n;
106
+
return()=>local++;
107
+
})(0);// Immediately invoked with an initial value of 0
108
+
109
+
console.log('incrementer value is:',incrementer());// incrementer value is: 0
110
+
console.log('incrementer value is:',incrementer());// incrementer value is: 1
111
+
console.log('incrementer value is:',incrementer());// incrementer value is: 2
112
+
113
+
// In the previous example, we used an IIFE (Immediately Invoked Function Expression) to create a closure,
114
+
// but we can achieve the same with regular functions.
115
+
functionsquare_v2(n: number): ()=>number{
116
+
return()=>n*n;
117
+
}
118
+
119
+
constpow64=square_v2(64);
120
+
constpow78=square_v2(78);
121
+
122
+
log(pow64());// 4096
123
+
log(pow78());// 6084
124
+
125
+
// Arrow Functions
126
+
constfactor=(n: number): number=>{
127
+
if(n===1)return1;
128
+
returnn*factor(n-1);
129
+
};
130
+
131
+
log(factor(8));// 40320
132
+
133
+
// Note: The factor function is recursive, meaning it calls itself.
134
+
135
+
// Arrow functions allow us to abbreviate expressions when they have only one parameter or a single expression.
136
+
constsquare_v3=(n: number): number=>n*n;
137
+
log(square_v3(4));// 16
138
+
// Note: We omit the curly braces, the return statement, and also the parentheses.
139
+
140
+
// Note 2: It's important to remember that arrow functions do not have their
141
+
// The following code is commented to avoid conflicts in Node.js environments.
142
+
// If you transpile it to JavaScript, it will run fine in a browser.
// alert(`${this.value} Please open the Browser Developer Tools.`);
150
+
// }, 1000);
151
+
// }
152
+
// };
153
+
154
+
//obj.advertisement(); // Alert: Rutadeprogramacion Exercice #2. Please open the Browser Developer Tools. and Logs in console: Rutadeprogramacion Exercice #2.
155
+
156
+
/* Note: Because arrow functions don't have `this`, the 'this' points to obj.
157
+
If we had used a regular function instead of the arrow function inside the setTimeout,
158
+
it would have alerted undefined or thrown a reference error. */
159
+
160
+
// Function Declarations: A function declaration defines a named function. It is hoisted, meaning it can be called before it is defined in the code.
161
+
functionsubtract(n: number,m: number): number{
162
+
returnn-m;
163
+
}
164
+
165
+
log(subtract(8,4));// 4
166
+
167
+
// Function Expressions: A function expression defines a function as part of a larger expression.
168
+
// It can be anonymous or named and is not hoisted.
speak.apply(person,["I","will","go","through","this","roadmap","until","the","end"]);// Niko: I will go through this roadmap until the end
370
+
speak.call(person,"But","first","I","need","some","exercise");// Niko: But first I need some exercise
371
+
372
+
/* Notice that apply allows you to pass an Array or the arguments object (array-like) as the list of arguments,
373
+
whereas call requires you to pass each argument separately. */
374
+
375
+
/* As we see in the previous obj.advertisement() example:
376
+
When using arrow functions, 'this' takes the value from the enclosing execution context's 'this' (that is,
377
+
'this' in arrow functions has lexical scope rather than the usual dynamic scope).
378
+
In global code (code that doesn't belong to any function), it would be the global object.
379
+
And it keeps that way, even if you invoke the function declared with the arrow notation from any of the other methods here described. */
380
+
381
+
/* The bind method of every function allows you to create a new version of that function with the context strictly bound to a specific object.
382
+
It is especially useful to force a function to be called as a method of an object. */
383
+
384
+
letperson2={name: 'Anna'};
385
+
386
+
functionsayHiLady(this: {name: string}): void{
387
+
log(`Hi ${this.name}`);
388
+
}
389
+
390
+
consthiAnna=sayHiLady.bind(person2);
391
+
hiAnna();// Hi Anna
392
+
393
+
/* This a possible output example for the previus logParagraphs() async function:
394
+
[
395
+
'Shank aliquip fugiat, cupim irure pig capicola kielbasa in mollit bresaola venison bacon aliqua. Alcatra aliqua jowl fatback pork chop in exercitation corned beef leberkas aliquip qui chicken consequat laboris. Swine shank porchetta tail quis in. Chuck dolore venison, picanha id swine cupim strip steak porchetta magna ball tip excepteur hamburger mollit ex. Picanha chislic irure strip steak sausage, in kevin ullamco. Magna pork belly tongue meatloaf elit tempor. Veniam meatball corned beef pork chop in.',
396
+
'Sint ut deserunt pork loin enim doner sunt chuck qui occaecat aliqua meatloaf spare ribs exercitation. Shank esse cupidatat, boudin cupim eiusmod tenderloin occaecat do drumstick cow ut meatball. Aute non pig esse ea capicola landjaeger beef ad qui minim in boudin. Shank non dolore adipisicing. Ham hock drumstick sint landjaeger minim. Dolor in nisi chicken sunt.'
0 commit comments