Having difficulty defining properties within a JavaScript class

Currently, I am trying to set properties within a JavaScript class:

class BookingReports extends ReportsInterface{ 
    var categoryID;
    constructor() {
        super();
    }

    init(CatID)
    {
        this.categoryID=CatID;            
    }
}

However, when attempting to do so, JavaScript is rejecting the variable and throwing an error stating "unexpected identifier".

https://i.sstatic.net/zCbbg.png

I am struggling to pinpoint what exactly is causing this syntax error. Could it be related to inheritance or the super keyword? I've even tried using binding within the entire declaration, but unfortunately, that approach is not resolving the issue.

Answer №1

var categoryID seems misplaced in the code. It is not common practice to declare variables at the class level; instead, variables should be declared within constructors or methods.

In this scenario, consider creating the property within the constructor rather than declaring a variable:

class BookingReports extends ReportsInterface{ 
    constructor() {
        super();
        this.categoryID = /*...some default value...*/;
    }

    init(CatID)
    {
        this.categoryID=CatID;            
    }
}

An init method may not be necessary here; typically, initialization should occur in the constructor itself. Therefore, your code could look like this:

class BookingReports extends ReportsInterface { 
    constructor(catID) {
        super();
        this.categoryID = catID;
    }
}

In the future, with advancements in the class fields proposal, it might become possible to "declare" properties at the class level directly:

// This will be valid if and when the class fields proposal progresses
class BookingReports extends ReportsInterface { 
    categoryID; // Property declaration example
    constructor(catID) {
        super();
        this.categoryID = catID;
    }
}

If the property is being initialized in the constructor anyway, using property declarations can help make the structure of your object clearer both to readers and JavaScript engines.

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

show an HTML webpage within a <div> container using AJAX technology

I am trying to include an HTML page named Introduction.html (located in the same folder as x.html) within div1. However, it does not seem to be loading properly. Below is a snippet of the code from x.html x.html <!DOCTYPE html> <h ...

Dependency of multiple objects in Webgl three.js

I'm struggling with object dependencies and need help. I want to create a setup where one object is dependent on two other objects. Essentially, when I modify the position of one parent object (like changing the y-Position), the dependent object (chil ...

Expo constants failing to load on web due to unresolved manifest object issue

When setting up Firebase Auth in my expo app (using Google Auth), I needed to store my firebase variables in a .env file containing API_KEYS, AuthDomain, and more. To access these environment variables, I utilized expo constants in my firebase.ts file. Ini ...

Checking for selected checkboxes in Django admin's action-select using jQuery/JavaScript

In my Django admin project, I want to check if any of the action-select check-boxes have been selected in the change_list.html file using jQuery/JavaScript. Initially, I tried the following code: $(".action-select"); This gave me an array of all the inpu ...

What could be hindering my jQuery function from running the Ajax script?

My jQuery function is supposed to retrieve two values, one for school/college and one for state, and send it to the controller class under the URL "Type&State". However, for some reason, the data is not being passed and nothing is shown in the console ...

Tips for handling an AJAX form submission with various submit buttons and navigating through Laravel routes

I am having trouble getting the form below to submit to the database. The issue arises when trying to use AJAX to submit the forms. The problem seems to occur at this point: $(i).submit(function(event) { Can someone help me figure out what I am doing wr ...

What is the reason for handlers not being able to work without passing parameters in React?

I recently made a discovery but I'm still puzzled about how it works. In the past, when creating React components, I used to follow this pattern: class App extends React.Component { state = { input: '' } onChangeHandler = event = ...

Combining the power of AngularJS with the versatility of sails

A project I'm working on involves utilizing sails.js for back-end and AngularJS for front-end. My plan is to use the Yeoman-angular generator https://github.com/yeoman/generator-angular to create the Angular app first. Once the front-end development i ...

Verification of input on ng-repeat table

I am working on an AngularJS app and have a table with ng-repeat where I have textboxes in td. I want to validate these textboxes so I tried using ng-form and ng-class, but I keep getting an invalid expression error. Here is my code: <input name ="abc ...

Guide to iterating through an object and generating child components in React.js

Struggling with a React.js loop through child component issue as a newcomer to the language. I have an object and want to create child components from its values. Any help would be greatly appreciated. var o = { playerA: { name: 'a', ...

Initiating a conversation from deep within a conversation using JQuery Mobile

I've been attempting to open a Dialog from within another Dialog, but so far no success. Take a look at the HTML code below: <a href="#popupDialog" data-rel="popup" data-position-to="window" data-role="button" data-inline="true" data-transition=" ...

AngularJS controller function fails to trigger

I have three directives, "parenta", "parentb", and "childb". The first two are siblings while the third one is a direct child of "parentb". When attempting to call a controller method from "parenta", it does not work. Strangely, calling a method on the "c ...

Eliminating the selected option from the dropdown menu after it has been added with Angular

HTML Code <!doctype html> <html ng-app="plunker"> <head> <meta charset="utf-8> <title>AngularJS Plunker</title> <link rel="stylesheet" href="style.css"> <script> document.write("<base href=&b ...

Trying to incorporate a PHP echo statement into the innerHTML of a button is ineffective

I have a piece of jQuery code that is not functioning as expected: $("#erase").click(function(){ $("#erase").attr("disabled", "disabled"); // Prevent double-clicking $(this).html("Erasing..."); $.post("erase.php", {target: $("#targ ...

Express router parameter matching

Let's consider a scenario where I have two routes - one with parameters and one without: /foo?bar /foo I aim to assign different handlers for these routes. Instead of the conventional approach, I am looking for a way to simplify the code. app.use(&a ...

Traverse the data() object in jQuery to retrieve both keys and values simultaneously

I have a data() object with some JSON content. Is there a way to iterate through the object and extract each key and value pair? Here's my current code snippet: function getFigures() { var propertyValue = getUrlVars()["propertyValue"]; $.getJSON( ...

Trying to assign a value to 'currentStatus' using "this" on an undefined property

In my attempt to display the state of formSubmit in vue js, I've encountered an issue. My lack of familiarity with using "this" has led to errors in the code, particularly when trying to indicate the status using "this.currentStatus". This is the cod ...

Having trouble with the Javascript denial of submit functionality?

I'm dealing with an issue in my code where form submission is not being denied even when the input validation function returns false. I can't figure out what's causing this problem. <script> function validateName(x){ // Validati ...

What is the best way to create a basic accordion using only certain table rows?

I am faced with the task of transforming a HTML table that lists various items. Each <tr> within the table contains a unique title element, but there are cases where rows can share the same title indicating their relation. My goal is to implement jQu ...

Utilizing a Buefy element without Vue.js integration

Is there a way to generate a Buefy notification without utilizing a Vue component? Specifically, I'm attempting to implement a Buefy notification within the axios interceptor below: import axios from "axios"; import { Notification } from "buefy/dist/ ...