6

Paracetamol.js💊| #17: ¿Qué imprime este código JavaScript?

 2 years ago
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.
neoserver,ios ssh client

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)

pic

Collapse

Expand

Author

Jan 18

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.

Comment button Reply


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK