When used Node.js
to import a class it would be:
var Comprobador = require('./comprobador.js');
let comprobador=new Comprobador();
But in pure JavaScript on the client side, how would it be?
When used Node.js
to import a class it would be:
var Comprobador = require('./comprobador.js');
let comprobador=new Comprobador();
But in pure JavaScript on the client side, how would it be?
In previous versions of JavaScript there was no way to include
JavaScript
either byimport
orrequire
.Newer versions added import and export functionality to satisfy this point by making use of standards like ES6 Modules , just be aware that currently, browser support for modules
ES6
is not very good. To improve this, compilation or transpilation tools are used, which would be the most recommended.With these tools it will be easier and the syntax will be similar in some cases to the one used in
Node
A base example to create a class in one file and import them into another. We create the Person class with a parameter in the constructor and a method
Or simply.
To call in the other file will suffice as in your example
In ES6 , if you have a class called:
and this class is in
/modulos
, you can import it by doing this:You can also import functions, for example if you have a file called
/modulos/Mates.js
:you can import functions like this: