Skip to content

Instantly share code, notes, and snippets.

@molavec
Last active February 2, 2018 21:08
Show Gist options
  • Save molavec/f68ee4a846ceefd671741cf7b82ce424 to your computer and use it in GitHub Desktop.
Save molavec/f68ee4a846ceefd671741cf7b82ce424 to your computer and use it in GitHub Desktop.
[Javascript ES6] Notas y tips acerca de Javascript #javascript

Clases

Class Declararion

The class expression is one way to define a class in ECMAScript 2015. There is two ways to define a class expresion:

class nombreDeLaClase {
  constructor(maker, price) {
    this.maker = maker;
    this.price = price;
  }
  getInfo() {
    console.log(this.maker + " costs : " + this.price);
  }
  toString() {
    return `${this.maker} costs : ${this.price}`;
  }
}

Class Expression

The class expression is one way to define a class in ECMAScript 2015. There is two ways to define a class expresion:

Named Class:

var nombreDeLaClase = class c {
    …
    console.log(c)
}

Unnamed Class:

var nombreDeLaClase = class c {
    …
    console.log(c)
}

Métodos

Hay tres tipos

Constructor method. - define el constructor

class nombreDeLaClase {
  constructor(maker, price) {
    this.maker = maker;
    this.price = price;
  }
  ...
}

Static method. - método estático

class Car {
	...
    static count() {
        console.log("I am static method");
    }
}

Car.count();
Car.count();

Prototype method. - Método de un objeto

  class Car {
      ...
      getInfo() {
          console.log(this.maker + " costs : " + this.price);
      }
  }

Importar desde otro archivo una clase

Metodo 1

Since you're also assigning the variable as a property of exports:

exports.ConversationModule = ConversationModule;

you'd have to call it like this:

var ConversationModule = require('./file').ConversationModule;
ConversationModule.sayhello();

Metodo 2

If you don't want to do that, assign the object to module.exports:

module.exports = ConversationModule;

And call it like this:

var ConversationModule = require('./file');
ConversationModule.sayhello();

Funciones

Function declaration

A Function Declaration defines a named function variable without requiring variable assignment. Function Declarations occur as standalone constructs and cannot be nested within non-function blocks. It’s helpful to think of them as siblings of Variable Declarations. Just as Variable Declarations must start with “var”, Function Declarations must begin with “function”.

function bar() {
    return 3;
}
bar() //3
bar  //function

Function Expression

A Function Expression defines a function as a part of a larger expression syntax (typically a variable assignment ). Functions defined via Functions Expressions can be named or anonymous. Function Expressions must not start with “function” (hence the parentheses around the self invoking example below)

//anonymous function expression
var a = function() {
    return 3;
}

//named function expression
var a = function bar() {
    return 3;
}

//self invoking function expression
(function sayHello() {
    alert("hello!");
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment