JavaScript ES6 Modules
Why Stack Overflow recommends it
Stack Overflow users cite it as a reference when asking or answering questions about using import/export and type="module" in browser JavaScript.
Recommended by 1 people across 1 different months, 2021 to 2022.
What it is
A short tutorial from the Web Dev Simplified channel that explains how ES6 modules work in JavaScript, covering import and export syntax and why modules help keep code clean and reusable. It is presented by Kyle Cook as a screen-recorded coding walkthrough.
What people said
Recommends Samathingamajig · 2021-06 · Stack Overflow · on “How do you declare a JavaScript Class Method outside of the class?”
Here's a screenshot from that video. [this video] As you can see, Kyle from Web Dev Simplified uses the user variable passed in by reference, not this . Your proper code (with this changed) is: class User { constructor(name, age) { this.name = name; this.age = age; } } function printName(user) { console.log(`User's name is ${user.name}`) } function printAge(user) { console.log(`User's age is ${user.age}`) } const user = new User("Bob", 11); printName(user); printAge(user); How are printName and printAge linked to the class User? They aren't "linked", when an object gets passed in with a .name or .age property it prints that (depending on the function) and if it doesn't it throws an error. It does
Cites PaulD · 2022-01 · Stack Overflow · on “Import in VSCode without using .mjs files”
I'm having trouble importing files in JavaScript. I am fairly new to the language and have followed a couple of VSCode tutorials that demonstrate and explain importing. Their importing seems to work out of the box, with one tutorial setting up his files like this, which worked for him: [LINK] I also made sure to set up my index.html properly to use ES6 module imports. My index.html looks like this: <!DOCTYPE html> <html> <head> <title>JS Modules</title> <script type="module" src="main.js"></script> </head> <body> </body> </html> But no matter what I do, the only way I can get VSCode to handle imports and not give this error SyntaxError: Cannot use import statement outside a module is by chan
Cites Soma Juice · 2022-05 · Stack Overflow · on “Importing javascript class using type=module”
I want to export a class to another js file, but i cant see any output on the console on my browser? i have been following the steps of this tutorial aswell as an other tutorial: [this video] and they show something similar to the code i have written. I told the browser that im using modules with type="module" aswell. Im sorry if i missed something but the posts i can find here: How do I include a JavaScript file in another JavaScript file? [LINK] deals with complicated scenarios and functions, i just need to import a Class class 1: import Rectangle from "./class2.js"; const rect = new Rectangle(100, 100); rect.printHeight(); class 2: export default class Rectangle { constructor(height, width) {
Critical happy_story · 2021-06 · Stack Overflow · on “How do you declare a JavaScript Class Method outside of the class?”
I was watching this video about modules, and this guy ( [this video] ), who I DO NOT recommend you ever watch, declared two functions outside of the class, which execute a console.log that prints out ${this.name} , a property declared inside the constructor() . First of all, I have never, ever seen that before, it makes zero sense to me, because how exactly does ${this} refer to the constructor() !? .. but anyway, it works for the guy, so I replicated the code 1:1 identical, and unsurprisingly, it's not working for me! It keeps telling me that {this.name} is not defined, which is to be expected, but how is it working for him!? Am I doing something wrong? Here's the code: class User { constructor(