Tips for implementing public fields in an ES6 class in a simple and clean manner

Can a public field be defined in an ES6 class as shown below:

class Device {

    public id=null;
    public name=null;

    constructor(id,name,token) {
        this.id = id;       // I want this field to be public
        this.name = id;     // I want this field to be public

        this.token = token; // this will be private
    }
}

I understand that it is simple to have private fields by placing them in the constructor (like the 'token' field in the example code above), but how can we define public fields?

Answer №1

Indeed, when you define a value to a property of this in a constructor, that field becomes public since ES6 classes do not support private fields.

class Example {
  constructor(type) {
    this.type = type;
  }
}

const example = new Example("Sample");
console.log(example.type); // "Sample"

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Modifying the input box value upon submitting a form

My query involves a form with multiple input fields. One of these inputs is for height, with the units specified in cm/inches. However, my API only accepts values in cm. So, if a user selects inches and submits the form, I need to covert the height value ...

Experiencing trouble with setting values in JavaScript?

I have my code set up to display numbers 170 and 122 using Javascript, but I'm not seeing the expected output. <!DOCTYPE html> <html> <body> <button onclick="UidToPost()">Get It</button> <script> ...

What's a quick way in Javascript to add a string to all elements in an array?

I'm working with an array = ["a", "b", "c"]; What I need to do is concatenate a string, let's say "Hello", to each value in this array. The desired output should look like this: ["Hello_a", "Hello_b", "Hello_c"] Is there a quicker way in java ...

Express identifies the user, however, the username is displayed exclusively on one specific page. This situation may indicate a potential cookie problem

I am in the process of developing an express app integrated with MongoDB. The issue I am facing involves a pug template for the navigation bar where I aim to display the user's name at the top upon logging in. Strangely, it only works on the page that ...

Is it possible to use a single function to both set the state and return JSX?

I'm currently grappling with creating a function that, when called, will update the state to an object {item: 'whatever the user types', list: [...list, todo]}. The challenge I'm facing is figuring out how to update the state and return ...

Utilizing JSON in a data-attribute

Hey all! I am facing a challenge with extracting data from a JSON object stored in a data attribute. While I have managed to extract the string within the data attribute, I am struggling to access the object itself. Below is the code snippet: HTML <a ...

Having trouble with installing the most recent versions of React App dependencies

After cloning a project from GitHub, I attempted to install dependencies using npm install, but encountered an error: npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: <a href="/cdn-cgi/l/email ...

Comparing the efficiency of Jquery draggable with thousands of elements versus updating the elements continuously

Currently, I am developing an application that involves dragging an image using Jquery's draggable utility. The image is accompanied by several overlay divs containing various components positioned based on pixel locations, sometimes reaching into the ...

Exploring directories for files using Node.js

Currently, I am attempting to locate specific files within a designated folder on Windows using node and grunt. I have a grunt task set up with a function to read a directory containing JSON files. However, when I execute the task, the code for reading th ...

utilize makeStyles to modify button text color

Initially, my button was styled like this: style={{ background: '#6c74cc', borderRadius: 3, border: 0, color: 'white', height: 48, padding: '0 30px', }}> It worke ...

Challenges with form validation

Hello everyone, I'm a newbie to JS and struggling with my code. It seems like everything should work, but it just won't. The issue seems to be with the phone number form validation. I've written code that, in theory, should do the job, but ...

Encountering ERR_CERT_AUTHORITY_INVALID when trying to attach a YouTube video URL in Angular

I'm currently working on a project using Angular 8. One of the requirements is to display 6 YouTube videos in a modal popup. To achieve this, I have implemented the following code: This is my HTML file code: <iframe [src]="safeSrc1" widt ...

The jQuery selector for items that do not contain

Have you ever wanted to hide list items based on user input in a text box? The ":contains()" jQuery selector can help you achieve this by allowing you to match elements containing specific text. I have a similar goal - I want users to be able to type in a ...

"Implementing AngularJS bidirectional data binding to dynamically link user inputs with corresponding fields

Having trouble automatically outputting data with angularJS. One of the great features of angular is two-way data binding, but I can't seem to bind input with a JSON file. What I want to achieve is if the user's input matches a key, the correspon ...

Exploring Multidimensional Arrays in JavaScript using Nested Loops

Let's consider an example array: var myArray = [ [1,2,3], [4,5,6], [7,8,9,10], [[11,12],[13,14,15]]]; for (var i = 0; i < myArray.length; i++){ for(var j = 0; j < myArray[i].length; j++){ for(var k = 0; k < myArray[i] ...

Send the data from a div to a different page using PHP

Is there a way to pass the value displayed in DIV id to another page? The data is retrieved from another page and shown within the given code. <div id="res" style="float: left; Padding-left: 3px; padding-right:13px;line-height: 15px;"> ...... ...

Mongoose encounters an issue: MissingSchemaError is thrown because the Schema has not been registered for the model

Encountering a perplexing error that I can't seem to resolve. I HAVE NAMED THE REF EXACTLY AS THE MODEL: MissingSchemaError: Schema hasn't been registered for model "ParticipantStatus". Here are the models in question: ParticipantStatus model: c ...

Showing data from a MySql database using an HTML table

I am currently working on a pop-up form that captures user data and stores it in a MySQL phpmyadmin table. My goal is to have this data displayed in an HTML table once the popup form is closed. After submitting the form, I am redirected back to the homepag ...

What are the steps to increase or decrease the quantity of a product?

Is there a way to adjust the quantity of products in the shopping cart? I would like to be able to increase and decrease the quantity, while also displaying the current value in a span tag. <a href="javascript:" id="minus2" onclick="decrementValue()" ...

What is the process for making a name tag that pops up when a model is clicked in a 3-D viewer?

After loading individual STL structures into my 3-D viewer using three.js, I would like the ability to click on each structure and have a name tag appear when hovering over it. I believe this functionality may be related to the coordinate point of the obj ...