Methods for bypassing a constructor in programming

I am working on a code where I need to define a class called programmer that inherits from the employee class.

The employee class constructor should have 4 parameters, and the programmer class constructor needs to have 5 parameters - 4 from the employee class and one specifically for the programmer class.

What is the best way to achieve this?

class employee {

     private id: string;
     private name: string;
     private department: string;
     private salary: number;

    constructor(id: string , name: string , department: string , salary: number) {

        this.id = id;
        this.name = name;
        this.department = department;
        this.salary = salary;

    }



    speak() {
        console.log(`ID = ${this.id} , Name = ${this.name} , Department = ${this.department} , Salary = ${this.salary}`);
    }
}

class programmer extends employee {

    private programmingLang: string;

    constructor() {
        super();
    }

    speak() {
        super.speak() , console.log(` , Programming Language = ${this.programmingLang}`);
    }
}

Answer №1

It appears that the child class constructor should be structured in this manner.

Constructor(id: string, name: string, department: string, salary: 
number, programmingLang: string) {
super(id, name, department, salary);
this.programmingLang = programmingLang;
}

The child class inherits all properties from its parent and also possesses specific properties unique to itself. Therefore, the child class constructor should accept all inherited properties as parameters, call the parent class constructor with those inherited properties as arguments, and then initialize its own specific properties using the 'this' keyword.

While I am not an expert in JavaScript programming, this is a general concept of inheritance that typically applies to object-oriented programming languages. Hopefully, this explanation proves helpful.

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

Refresh the pagination in a jQuery DataTable

I have incorporated DataTable for pagination within my table. The data in the table is loaded through ajax requests, and I am utilizing my custom functions to populate the table manually by interacting with its DOM elements. However, I am facing an issue ...

Hide HTML div on click

Why does the button disappear when I click on it, but the status refreshes? Javascript $( ".refreshstatus" ).click(function(){ $( ".navplayers" ).load('stats.php'); }); CSS .refreshstatus{ font-family:'Noto Sans'; font-w ...

Retrieve the place_id associated with the address components

Having trouble obtaining the place_id of address_components using Google place autocomplete? The JSON data only includes long_name, short_name, and types. Take a look at my code snippet below: var object_location = document.getElementById('object_loc ...

utilizing a spacemouse in conjunction with autodesk forge

Recently, I acquired a 3dconnexion spacemouse and have been attempting to configure it to work with the forge viewer. Fortunately, I came across some JS samples in the SDK for three.js that worked flawlessly. It seems both forge and three.js utilize webgl ...

Unable to locate the accurate information

Every time I run the cycle, there should be a match with the specified parameters and the message "OK" should appear. However, I am always getting a result of "No". request( { url: 'http://localhost:5000/positions/get', metho ...

What steps can I take to resolve the 'Object may be null' error in TypeScript?

I am facing a similar issue to the one discussed in this thread, where I am using Draft.js with React and Typescript. After following the code example provided in their documentation, I encountered the 'Object is possibly 'null'' error ...

Tips for updating the css class for multiple DOM elements using jQuery

I am currently attempting to modify the class property of a specific DOM element (in this case, a label tag) using jQuery version 1.10.2. Here is the code snippet I have written: var MyNameSpace = MyNameSpace || {}; MyNameSpace.enableDisableLabels = (f ...

Expanding/Combining entities

I'm facing an issue while trying to Extend/Push/Merge an object using AngularJS. The problem arises when I attempt to extend the object, as it adds a new object with an Index of 0 and subsequent additions also receive the same index of 0. //Original ...

Using Regular Expressions to eliminate any characters that are not directly adjacent to numbers (beside specific characters)

My function generates a result that looks like this: given output For comparison, consider the example string below: var string = '    111   1   1\n 1111111 1 \n   &nb ...

How can I send a JavaScript variable to a PHP function using an Ajax call?

I'm having trouble implementing an AJAX search form in WordPress that retrieves posts based on the search term being present in the post title. Below is the PHP function I've created for this purpose: function get_records_ajax($query) { $arg ...

ParsleyJS always seems to miss the mark when it comes to

I had previously sought advice on a different JavaScript/jQuery form validation library but was told it was outdated, so I switched to Parsley. While Parsley allowed me to make some progress, I still encountered issues with its functionality. Specifically, ...

The getter method in the Vuex store object seems to be returning varying values when accessing nested properties

Currently, my Vuex store is being used to store a user object. This code snippet is a getter function for the user object: getters: { user: (state) => state, isAuthenticated: state => { console.log("user object", state); ...

Give GetElementsByClassName a shot

Hey, have you tried using js ref_doc_getelementsbyClassName? "I keep getting the error message 'Uncaught TypeError: Cannot set property 'value' of null' " Check out this HTML code snippet: <input type="text" cla ...

Angular 5 and the benefits of concurrent requests

My goal is to execute multiple requests in parallel, fetch the results, and combine them. To achieve this, I have implemented the following function: getStudent(query): Observable<any> { const code = this.http.get( `http://localhost ...

Preventing template rendering in Angular until an event is triggered - but how?

I am currently working on a directive that functions well, but I had to resort to using inline template code in order to delay rendering until the click event occurs. However, I believe it would be more streamlined if I could assign the directive template ...

Creating unique identifiers and names for dynamically generated editable HTML table cells using JavaScript

Clicking the button will trigger the GenerateTable() function to dynamically create a table based on selected options from the drop-down menu as column headings. Below is the JavaScript code, along with instructions on how to assign IDs and names to each t ...

Struggling to establish a functioning proxy in my React and Node application

In the process of developing a react client app with a node.js express backend, I have encountered an issue related to project structure. The client app includes a proxy configuration in its package.json file: "proxy": "https://localhost:5000" However, ...

Exclude mock files from webpack configuration in AngularClass/angular2-webpack-starter for production builds

I've encountered some obstacles while working with the AngularClass/angular2-webpack-starter framework, specifically in configuring its webpack settings. My goal is straightforward, yet I've been struggling to achieve it. In my project directory ...

Encountering AngularJS promise data parsing issues

I am trying to work with promises in AngularJS. However, I encountered an error while parsing the backend response in AngularJS. What could be the issue here? This is the HTML code: <div ng-app="clinang" ng-controller="pacientesCtrl"> <a ...

JS showcase of object literals and their corresponding properties

Just starting out with Javascript and eager to learn about arrays of objects. I'm currently exploring how to display an object along with its properties. Here's an example showcasing the colors of different fruits: var fruitColor = {'apples ...