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

126
Vistas
How to make objects in javascript

How can I do this in javascript? (Adding getters and setters properly)

public class User {

    private UUID uuid;
    private String nickName;
    private double coins;
    private int level;
    private float exp;

    public User(UUID uuid) {
        this.uuid = uuid;
        this.nickName = null;
        this.coins = 0D;
        this.level = 1;
        this.exp = 0F;
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

In JavaScript you don't need to create first a class to create an object. A Javascript object is an associative array that can contain also functions:

// example of JavaScript object
const user = {
   uuid: "user-identifier",
   nickname: null,
   // ...
}

Of course you can define a function that returns an object. You can think at that function as a constructor if you want, but it is just a function that returns an object. Function closures allow you to define private variables.

function User (uuid) {
   let coins = 0;  // Visible only inside this function
   return {
      uuid: uuid,
      nickname: nickname,
      getCoins: function () { return coins }
   }
}

Inheritance is prototypal in Javascript: you take an object and you create a new object that inherits all its properties.

const parentObject = {/*key-value-pairs*/};
const childObject = Object.create(parentObject);

JavaScript has a new operator that makes it easier to handle inheritance and makes a function look like a class.

Recently also a class statement has been added to JavaScript but it is just sintactic shugar for the prototype pattern.

With that in mind, I suggest you read this article for a thorough introduction to JavaScript objects.

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