Your Task
function does not have a hey
property. The hey
property belongs to the prototype that would be assigned to an instance of New Task
, so:
const t = new Task();
t.hey();
Alternatively, if you prefer to use it as Task.hey();
, make it static
:
class Task {
static hey() {
// ^^^^^^-----------------
alert("hey");
}
}
Now, Task.hey()
will work because hey
is a static method (a method of Task
, not of instances of Task
).
If you try using this plunker on the latest version of Chrome, it should demonstrate that it's functioning with an instance method. Here are the source files:
index.html
:
<!DOCTYPE html>
<html>
<body>
<script type="module" src="task.js"></script>
<script type="module" src="app.js"></script>
</body>
</html>
task.js
:
class Task {
constructor() {
}
hey() {
document.body.appendChild(
document.createTextNode("hey")
);
}
}
export default Task;
app.js
:
import Task from './task.js';
const t = new Task();
t.hey();
If you use this one on the most recent version of Chrome, it'll show that it's working (with a static method). Here's the source:
index.html
(same as above)
task.js
:
class Task {
constructor() {
}
static hey() {
document.body.appendChild(
document.createTextNode("hey")
);
}
}
export default Task;
app.js
:
import Task from './task.js';
Task.hey();
Please note that module support in Chrome is very new; you must have the latest updates installed (as of late October 2017).