How to Retrieve Child Class Property from Static Parent Class Method in JavaScript

How can we implement a parent class with static methods that access a property defined in the child class using JavaScript? Below is an example code snippet illustrating this concept:

class Parent {
    static greet() {
        return `Hi ${this.username}!`;
    }
}

class Child extends Parent {
    static username = "Jim";
}

Child.greet();

Answer №1

To access properties of the class (including inherited ones), use this. To set properties of the subclass, follow the same process as you would with any other object:

class Base {
   static greet() {
      return `Hi ${this.otherName}!`; // << make sure to include this here
   }
 } 

class Child extends Base {  } 

Child.otherName = "Jim"; // << since there are no static properties yet, we need to replicate that behavior

name is not a good property name because it conflicts with the internal function.name ...

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

Sending an AJAX request to submit a form and receiving a response

I am in the process of developing a Rails application and I am seeking a way to submit a form using Ajax. This functionality is crucial as I want the form submission to occur without causing a full page reload. Initially, I tried using form_remote_tag but ...

The promise is unexpectedly fulfilled ahead of schedule without returning the expected value from an AXIOS call

My current challenge involves making a request to a service that rapidly generates multiple strings. The problem lies in returning a promise from this service, as I lack control over the string-generation process. It is crucial for me to return a promise ...

guide on extracting values from several selectboxes and transferring them into an array

I am currently using a multi-select box that requires the ability to add and remove values, creating a comma-separated string array. To demonstrate my issue, I have created a Plunker: Plunker var msaskArray = []; $scope.$watch("MSASK", function (new ...

executing a function from one controller within another controller

I am currently working on a modal where, upon clicking the delete button, I need to execute the delete() function from controller A within controller B https://i.sstatic.net/HJhGT.png As part of my refactoring process, I am updating the Todo App example ...

Sending an AJAX request to a Symfony controller's URL

Recently, I encountered an issue with integrating a piece of code from backend.php into my Symfony controller. In the initial setup, I had an AJAX call in a JS file that interacted with backend.php to test some functionality. function postRequest() { var ...

Encountered an issue while trying to implement CORS using yarn

Currently experiencing the following issue: Error message: An unexpected error occurred "/home/vagrant/.cache/yarn/v1/npm-cors-2.8.4-2bd381f2eb201020105cd50ea59da63090694686/.yarn-metadata.json: Unexpected end of JSON input". Some important points to c ...

Unresponsive UpdatePanel and UpdateProgress components causing frustration

When using OnSelectedIndexChanged in this way: <asp:DropDownList ID="ddl1" AutoPostBack="true" OnSelectedIndexChanged="Test_SelectedIndexChanged" runat="server"></asp:DropDownList> The UpdatePanel and UpdateProgress function correctly, displ ...

Clicking on the search box will remove the item that is currently displayed

Currently, I am working on creating a dropdown menu that includes a text box. The goal is for the items to appear when the text box is clicked, and for the selected item to turn green once clicked and then display in the text box. I'm interested in fi ...

Monitoring variables in different AngularJS controllers

I have a component named histogram demo which includes a distinct controller with a variable known as $scope.selectedElements. I aim to monitor this variable in the primary appCtrl controller. How can I achieve access to this variable without using $rootSc ...

Saving JSON format in VueX State Management

I'm relatively new to using Vue/VueX and I am exploring methods for storing JSON data in the VueX state. Initially, it seemed like a simple task: state { jsonthing: { ... } } However, I encountered an issue where getters return an Observer type ins ...

The React class component is throwing an unexpected error with the keyword 'this'

I encountered an error stating "Unexpected keyword 'this'" while attempting to update the React state using Redux saga. Could someone shed light on what's wrong with the code below and how I can fix it? class Welcome extends React.Component ...

Struggling to interpret the array of objects retrieved from a call to UrlFetchApp.fetch in Google App Script

When UrlFetchApp.fetch is called, it provides an array of objects that are stored in the variable 'results': var results = [{"Category 1": "Benefits/Compensatio", "Category 2": "Compensation", "Category 3": "Recognizing You", "Processing Team": ...

What is the best way to ensure that my image gallery adjusts its width to be responsive to the div

I am facing an issue with my image gallery named slider1_container. The style properties are defined in the HTML code as follows: <div id="slider1_container" style="position: relative; top: 0px; left: 0px; width:700px; height: 600px; background: #1 ...

Rendering a Pop-up for a Single Array Item Retrieved from an API in ReactJS

Apologies if my wording is unclear, this is my first question, and I'm relatively new to JS/React... I have successfully mapped through an array from an API call and displayed the necessary information. How can I display a popup for a single element ...

Substitute regular expressions with several occurrences by their respective capture groups

I am attempting to use JavaScript to extract only the link text from a string and remove the href tags. The expected behavior is as shown below: <a href='www.google.com'>google</a>, <a href='www.bing.com'>bing</a> ...

The onBlur function is preventing link clicks from being registered

My Navigation Bar includes a React-InstantSearch: <SearchBox /> component that provides Autocomplete functionality. The SearchBox triggers an onChange event to display suggestions, and an onBlur event to hide the box when exiting the search field. Ho ...

Exploring key value pairs dynamically in JavaScript

I have a JSON object containing HTTP request information: "http": { "method": "POST", "headers": [{ "content-type": "application/json" }, { "Authorization": "KKYZASSHUYTRJ" }], "url": "http://localhost:8888/download/con ...

Internet Explorer's incompatibility with the window.location.href function

I'm having an issue with navigating to a new page using a callback from an AJAX post request specifically on Internet Explorer. Here is the code snippet causing the problem: $.ajax({ type: "POST", url: phpUrl, data: data, async: ...

Unexpected provider error in AngularJS when using basic module dependencies

I am encountering an issue with my module setup involving core, util, and test. The util module has no dependencies and one provider The test module depends on util and core, and has one controller The core module depends on util, and has a provider that ...

Maximum number of days that can be selected with Bootstrap Datepicker

I currently have a datepicker set with the multidate option and I am looking to specify a maximum number of days that users can select, say 5 days. Once a user has selected 5 days, any additional days should become disabled dynamically. How can this be a ...