0

I have an app.js and another Highlight.js file.

In Highlight.js I have the following code:

export default class Highlights {
function foo() { alert(1) };
}

and in app.js I try to call the function foo as follows:

import highlight from './custom/HighlightingObjects.js';
highlight.foo();

But already at compling this via webpack I get an error message, that there is an unexpected token at:

function foo() { alert(1) };

How can I handle this?

4
  • 2
    1. That's not how you define methods inside a class, hence the error Commented Jun 20, 2020 at 21:02
  • 1
    3. foo, when fixed, will be a method, so you can't call it like that, you should instantiate the class first like so: let instance = new Highlights(); then use foo like so: instance.foo(); Commented Jun 20, 2020 at 21:05
  • 4. You mentioned that the file containg the class is called Highlight.js, so why are you importing from './custom/HighlightingObjects.js', please verify the path is correct Commented Jun 20, 2020 at 21:06
  • 1
    @ibrahimmahrir If you're gonna answer a question please just post it as an answer and not as a comment. Commented Jun 20, 2020 at 21:10

1 Answer 1

1

foo is method of class. Instead

export default class Highlights {
  function foo() { alert(1) };
}

you need write

export default class Highlights {
  foo() { alert(1) };
}
Sign up to request clarification or add additional context in comments.

3 Comments

You got one, two to go (the import is not correct and the way he is using foo is also not correct).
Import is correct because 'export default'. In import name does not matter.
Also, OP wants to use the the method of the class like a static one, not a normal one.

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.