DEV Community

Cristian Fernando
Cristian Fernando

Posted on

3

Paracetamol.js💊| #185: Explica este código JavaScript

Explica este código JavaScript

Dificultad: Básico

function Student(nombre, apellido, edad){
  this.nombre = nombre;
  this.apellido = apellido;
  this.edad = edad;
}

Student.prototype.getNombreCompleto = function(){
  console.log(this.nombre + " " + this.apellido);
}

const juanito = new Student("Juan", "Ramirez", 26);
console.log(juanito.getNombreCompleto()); //??
Enter fullscreen mode Exit fullscreen mode

A. undefined undefined
B. null null
C. Juan Ramirez
D. SyntaxError Student.propotype.getNombreCompleto is not a function

Respuesta en el primer comentario.


Respuesta:
C. Juan Ramirez

Javascript es un lenguaje orientado a prototipos, desde su creación en 1995 fue concebido de esta manera, entonces podemos usar funciones como clases para abstraer la lógica del programa.

Quizá lo mas curioso de esta sintaxis es la creación de métodos usando la palabra reservada prototype que añade el método getNombreCompleto a todas las instancias creadas con Student.

La instanciación de objetos se hace como si se tratase de clases de toda vida. De hecho la sintaxis de clases agregada en ES6 al lenguaje es solo sugar syntax ya que por dentro todo se hace con prototipos.

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay