Paracetamol.js💊| #17: ¿Qué imprime este código JavaScript?
source link: https://dev.to/duxtech/paracetamoljs-17-que-imprime-este-codigo-javascript-42gb
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Paracetamol.js💊| #17: ¿Qué imprime este código JavaScript?
Paracetamol.js (19 Part Series)
¿Qué imprime este código JavaScript?
String.prototype.giveLydiaPizza = () => {
return "Just give Lydia pizza already!";
};
const name = "Lydia";
name.giveLydiaPizza();
Enter fullscreen mode
Exit fullscreen mode
- A: "Just give Lydia pizza already!"
- B:
TypeError: not a function
- C:
SyntaxError
- D:
undefined
Respuesta en el primer comentario.
Discussion (1)
Collapse
Expand
La respuesta:
A: "Just give Lydia pizza already!"
String
es el contructor que tiene JavaScript para gestionar las cadenas de texto. En el ejemplo se agrega la función giveLydiaPizza
al prototipo de las cadenas, con ello, esta función estará disponible para todas las cadenas.
Si intentamos hacer algo como lo siguiente:
String.prototype.giveLydiaPizza = () => {
return "Just give Lydia pizza already!";
};
const bool = true;
console.log(bool.giveLydiaPizza()); //TypeError: bool.giveLydiaPizza is not a function
Enter fullscreen mode
Exit fullscreen mode
Obtendremos un error, giveLydiaPizza
solo se puede usar con un string
.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK