Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

104
Vistas
Are we declaring a function as a method on a variable?

I have been learning JS on my own and completed an 8 hour course on the basics. Now I am following another course where the lecturer, as it seems to me, is creating a function using dot notation. I am not sure if this is what is happening and I am having a hard time understanding exactly what it is doing.

function Person(fName, lName) {
    this.firstName = fName
    this.lastName = lName
}
const person1 =  new Person("Bruce", "Wayne")
const person2 =  new Person("Bat", "Man")

person1.getFullName = function () {
    return this.firstName + ' ' + this.lastName
}

console.log(person1.getFullName())

The part I am confused about is:

person1.getFullName = function () {
    return this.firstName + ' ' + this.lastName

}

I see in the Person() function we are creating an object called person1 using "this" and a key to pass in parameters as values for those keys. However it looks to me like there is a function being assigned to the person1 object and what I am assuming is the name of the function, looks like a method on the person1 object using dot notation. Can anyone explain how this works?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

The line

person1.getFullName = ...

is a normal assignment statement. It assigns the value on the right hand side to the property getFullName of the object in person1. If the property doesn't exist yet it will be created.

Functions are values (objects) like any other in JavaScript and thus they can be used everywhere were a value is expected.

So yes, person1.getFullName will contain a function and it can be called like any other function: person1.getFullName().

about 4 years ago · Juan Pablo Isaza Denunciar

0

Objects have properties.

Properties have values.

Functions are a value.

If the value of a property is a function then we call that a method.

That's all there is to it.

about 4 years ago · Juan Pablo Isaza Denunciar

0

function Person(fName, lName) {
    this.firstName = fName
    this.lastName = lName
}
const person1 =  new Person("Bruce", "Wayne")
const person2 =  new Person("Bat", "Man")
 
person1.getFullName = function () {
    return this.firstName + ' ' + this.lastName
}

console.log(person1.getFullName())

Basically a new property called getFullName is created for the object person1. This new property is defined to return the first name and the lastname of the person1 object. So when you call console.log(person1.getFullName()) this will print the firstname and the lastname of the person1.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda