This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Data | |
let data = [ | |
{ | |
name: 'John', | |
SSID: 1 | |
}, | |
{ | |
name: 'John', | |
SSID: 2 | |
}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let my_array = ['Elem1', 'Elem2', 1, 2, 'Elem3', 'Elem2', 'Elem4', 1, 5]; | |
let filtered = my_array.filter( (element, index, thisArray) => thisArray.indexOf(element) === index); | |
console.log(my_array); | |
console.log(filtered); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Snippet for Promises (ES6) | |
// Create a Promise that is resolved and handle it using "then" methods | |
let p = new Promise( (resolve, reject) => { | |
setTimeout( () => { | |
resolve("Good to go!"); | |
}, 1000); | |
}); | |
p.then( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Snippet for rest parameters and spread operator (ES6) | |
// Rest parameters allow you to capture and indefinite number of parameters | |
// passed to a function | |
let sum = function(...args) { | |
// args is an Array with the parameters | |
return args.reduce( (prev, curr) => prev + curr); | |
} | |
console.log(sum(1, 1, 1, 1, 3)) // 7 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Snippet for arrow functions (ES6) | |
// Example of a normal arrow function | |
let substract = (a, b) => { | |
return a - b; | |
} | |
// Example of a one-line arrow function | |
let add = (a, b) => a + b; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Snippet for IIFE | |
// An IIFE (Immediately Invoked Function Expression) is a JavaScript function | |
// that runs as soon as it is defined. They can be written in different ways, | |
// though a common convention is to enclose both the function expression and | |
// invocation in parentheses. | |
var sulphuricAcid = (function(){ | |
var corrosive = true; | |
var pH = 2; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Snippet for template strings (ES6) | |
let firstName = "Santi"; | |
let lastName = "Puch"; | |
// Instead of doing this... | |
console.log("My name is " + firstName + " " + lastName); | |
// Use this... | |
console.log(`My name is ${firstName} ${lastName}`); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Snippet for currying | |
// Currying can be described as transforming a function that takes N arguments | |
// so that it can be called as a chain of N functions each taking a single argument. | |
let greetings = from => | |
message => | |
recipient => | |
'Dear ${recipient},\n\t${message}\n${from}.'; | |
let person = greetings("Jerry"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Snippet for closures | |
// A closure is a special kind of object that combines two things: a function, | |
// and the environment in which that function was created | |
function Person(name) { | |
var _name = name; // Private variable name | |
this.getName = function() { | |
return _name; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Snippet showing inheritance through prototypes | |
var Animal = function(type){ | |
this.type = type; | |
}; | |
Animal.prototype.eat = function(){ | |
console.log("They eat food to survive."); | |
}; | |
var Mammal = function(){}; |
OlderNewer