js import class code example

Example 1: javascript import class

//import it
import Example from './file2';
//Create an Instance
var myInstance = new Example()
myInstance.test()

Example 2: node import all functions from file

//we are in ./utils/dbHelper.js, here we have some helper functions
function connect() {
  // connect do db...
}

function closeConnection() {
  // close connection to DB...
}

//let's export this function to show them to the world outside
module.exports = {
  connect(),
    closeConnection()
};

// now we are in ./main.js and we want use helper functions from dbHelper.js
const DbHelper = require ('./utils/dbHelper'); // import all file and name it DbHelper
DbHelper.connect(); // use function from './utils/dbHelper' using dot(.)

// or we can import only chosen function(s)
const { connect, closeConnection } = require ('./utils/dbHelper');
connect(); // use function from class without dot

Example 3: javascript import

import { module } from "./path"; // single module
import Module from "./path"; // default export

import Module, { module } from "./path"; // both

Tags:

Html Example