Skip to content

Instantly share code, notes, and snippets.

View rxluz's full-sized avatar

Ricardo Luz rxluz

View GitHub Profile
const animals = ["elephant", "monkey", "snake", "lion"];
for (let index = animals.length; index > 0; index--) {
animals[index] = animals[index - 1];
}
animals[0] = "macaw";
console.log(animals); // will return [ 'macaw', 'elephant', 'monkey', 'snake', 'lion' ]
@rxluz
rxluz / DESIGN_PATTERNS_factoryFunctions.js
Created January 19, 2019 08:00
JS Design Patterns: Factory Functions, see more at: https://medium.com/p/2adfe878e949
const greet = name => ({
sayHello: () => `Hello ${name}!`,
sayGoodbye: () => `Goodbye ${name}!`,
});
const run = () => {
const greetRicardo = greet("Ricardo");
console.log(greetRicardo.sayHello());
console.log(greetRicardo.sayGoodbye());
@rxluz
rxluz / creatingObjectsWithClass.js
Last active January 19, 2019 07:57
JS Design Patterns: Factory Functions, see more at: https://medium.com/p/2adfe878e949
class greet {
constructor(name) {
this.name = name;
}
sayHello() {
return `Hello ${this.name}!`;
}
@rxluz
rxluz / objectLiteralExample.js
Last active January 19, 2019 07:55
JS Design Patterns: Factory Functions, see more at: https://medium.com/p/2adfe878e949
const greetRicardo = {
sayHello() {
return `Hello Ricardo!`;
},
sayGoodbye() {
return `Goodbye Ricardo!`;
},
};
class Directory {
constructor(name) {
this.setName(name);
this.children = [];
return this;
}
add(element) {
this.children.push(element);
class Car {
constructor({ model, year, manufacturer }) {
this.setYear(year);
this.setManufacturer(manufacturer);
this.setModel(model);
}
setModel(model) {
this.model = model;
}
class User {
constructor({ name, continent, email }) {
this.setName(name);
this.setEmail(email);
this.setContinent(continent);
}
setName(name) {
this.name = name;
}
class User {
constructor({ name, continent, email, allowCookies = true }) {
this.setName(name);
this.setEmail(email);
this.setContinent(continent);
if (continent === "Europe") {
this.setAllowCookies(allowCookies);
}
}
class SimpleEmailValidationService {
isValid(email) {
return email.indexOf("@") > 2;
}
}
class SimpleZipCodeValidationService {
isValid(zipCode) {
//A98 HNA23
return zipCode.length === 8;
class SimpleEmailValidationService {
isValid(email) {
return email.indexOf("@") > 2;
}
}
class SimpleZipCodeValidationService {
isValid(zipCode) {
//A98 HNA23
return zipCode.length === 8;