Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript extended class how to access parent imported function?

Tags:

javascript

I am extending a javascript class which has an import myParseFloat. An error is thrown that a child class does not have access to myParseFloat. What is the best approach to pass this function to a class extending a class? Importing the functions additionally on the child seems the easiest, but I am guessing incorrect. I would like to access myParseFloat directly, but i could use this.myParseFloat if needed.

TableView.js

import {myParseFloat, round2} from '../../lib/math';

export default class TableView {
    constructor(model){
        console.log(myParseFloat('1.2333445'));


    }

}

DataTableView.js

import TableView from './TableView';

export default class DataTableView extends TableView {
    constructor(model) {
        super(model);
        console.log(myParseFloat('1.2333445')); //myParseFloat is not available

    }
}
like image 259
Iannazzi Avatar asked Sep 07 '25 16:09

Iannazzi


2 Answers

Imports in javascript are exclusive to the module, so you'll need the line in both files.

like image 139
helion3 Avatar answered Sep 10 '25 07:09

helion3


Actually there is a way to achieve this

import {myParseFloat, round2} from '../../lib/math';

export default class TableView {
    constructor(model){
        this.myParseFloat = myParseFloat;
        this.round2 = round2;
    }

}

And in child class

import TableView from './TableView';

export default class DataTableView extends TableView {
    constructor(model) {
        super(model);
        console.log(this.myParseFloat('1.2333445')); 
    }
}
like image 42
Brijesh Singh Avatar answered Sep 10 '25 08:09

Brijesh Singh