Forgot Your Password?

Choose Your Plan

Start Building Real Apps

Pay Monthly

12

Pay Yearly

10
2 months free
Top shelf web developer training.

Guided Paths

Follow our crafted routes to reach your goals.

Courses

Premium content to build real apps.

Code Tutorials

Examples to follow and learn from.

Javascript modules syntax ES6

Introduccion

Trabajando POO ES6 en node.js tarde o temprano llega la hora de dividir el codigo para tenerlo mejor estructurado. en los siguiente ejemplos veremos algunas mejores practicas:

  • Funciones
  • Constantes
  • Clases o Multiple clases
  • Modulo index [in-progress]
  • Default [in-progress]
  • JSON [in-progress]
    • as ?? [in-progress]
  • Constantes [in-progress]

Export

Funciones

// module "aritmetica.js"
export function sumar(x, y) {
  return x + y;
}
export function restar(x, y) {
  return x - y;
}

// module "main.js"
import {sumar, restar} from './aritmetica';
sumar(2, 2); // 4
restar(2, 2); // 0

Constantes

// module "number.js"
export const ONE = 1;
export const TWO = 2;
export const THREE = 3;

// module "main.js"
import {ONE, TWO, THREE} from './number';
console.log(ONE); // 1
console.log(TWO); // 2
console.log(THREE); // 3

Clases o Multiple clases

Simple clases

// module "article.js"
export class Article {}

// module "main.js"
import { Article } from './publicacion';
let article = new Article();
article instanceof Article; // true

Multiple clases

// module "article.js"
export class Article {}
export class Post {}

// module "main.js"
import { Article, Post } from './publicacion';
let article = new Article();
let post = new Post();
article instanceof Article; // true
post instanceof Post; // true