Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

124
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!