0

I want to use inheritance between classes that are in separate .js (but same folder), but i have this errors

Uncaught SyntaxError: Unexpected identifier

Uncaught ReferenceError: TableWithFunction is not defined

Here goes my code:

// Table.js

export default class Table {

    constructor(name, h_cols) {
       table_id = name + 'table';
       hidden_cols = h_cols;
       table = setTable();
    }

    setTable() {...};

    setColumnDefs() {...};

{

// TableWithFunction.js

import Table from 'Table';

class TableWithFunction extends Table {

    constructor (name, h_cols) {
        super(name, h_cols);
        selected_id = name + 'Selected';
    }
    //More methods ..
}

//test.blade.php (im using laravel framework)

<script src="{{ url('js/TableWithFunction.js') }}" ></script>
<script>
    roleTable = new TableWithFunction('role',[2, 4]);
    userTable = new TableWithFunction('user',[3]);
</script>

I also tried to do import Table from 'Table.js'; instead of import Table from 'Table';

And i tried changing Table.js using class Table { ... } export default Table instead of export default class Table {...} and it didn't fix it, of course.

3
  • Possible duplicate of ES2015 import doesn't work (even at top-level) in Firefox Commented Dec 11, 2018 at 19:28
  • Can you try with import Table from '.\Table';? Commented Dec 11, 2018 at 19:37
  • <script src="{{ url('js/TableWithFunction.js') }}" type="module"></script> Commented Dec 11, 2018 at 19:40

1 Answer 1

1

To use ES6 modules in a capable browser the importing script needs to be included with type="module".

<script src="{{ url('js/TableWithFunction.js') }}" type="module"></script>

This prevents browsers not ready for modules to load code which would only throw an error.

The opposite is also available:

<script src="only-for-ie.js" nomodule></script>

This script will be ignored by browsers capable of handling ES6 modules.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks connexo, i resolve the first error, but still have Uncaught ReferenceError: TableWithFunction is not defined. I tried to do <script> $(document).ready(function() { roleTable = new TableWithFunction('role',[2, 4]); userTable = new TableWithFunction('user',[3]); }); </script> but still doesn'y work
Any JS that wants to use TableWithFunction will have to import it (which also means you'll have to add an export in your TableWithFunction.js. Modules never create global variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.