├── historia-conceitos ├── closure.js ├── currying.js ├── funcoes-primeira-classe-ordem-maior.js ├── hoisting-funcoes.js ├── hoisting-variaveis.js ├── imutabilidade-filter.js ├── imutabilidade.js ├── tipagem-dinamica.js └── tipagem-fraca.js └── tipos-e-variaveis ├── escopo └── tipos-escopos.js ├── tipos ├── array.js ├── boolean.js ├── functions-contexto.js ├── functions.js ├── null.js ├── number.js ├── object-funcoes.js ├── object.js ├── string.js ├── symbol.js └── undefined.js └── variaveis ├── 1-variaveis.js ├── 2-1-variaveis.js ├── 2-2-variaveis.js ├── 2-3-variaveis.js └── 3-variaveis.js /historia-conceitos/closure.js: -------------------------------------------------------------------------------- 1 | (() => { 2 | const exemplo = "Essa variável"; 3 | 4 | const funcao1 = () => { 5 | console.log(`Será lembrada aqui: ${exemplo}`); 6 | 7 | return () => { 8 | console.log(`Aqui também: ${exemplo}`); 9 | 10 | return () => { 11 | console.log(`Acredito que já entendemos o que é Closure: ${exemplo}`); 12 | }; 13 | }; 14 | }; 15 | 16 | funcao1()()(); 17 | })(); 18 | -------------------------------------------------------------------------------- /historia-conceitos/currying.js: -------------------------------------------------------------------------------- 1 | (() => { 2 | /** 3 | const soma = (a, b) => a + b; 4 | 5 | soma(2, 2); 6 | soma(2, 3); 7 | soma(2, 4); 8 | soma(2, 5); 9 | */ 10 | 11 | const soma = a => b => a + b; 12 | const soma2 = soma(2); 13 | 14 | soma2(2); 15 | soma2(3); 16 | soma2(4); 17 | soma2(5); 18 | })(); 19 | -------------------------------------------------------------------------------- /historia-conceitos/funcoes-primeira-classe-ordem-maior.js: -------------------------------------------------------------------------------- 1 | (() => { 2 | const getName = () => { 3 | return "Guilherme Cabrini da Silva"; 4 | }; 5 | 6 | const log = value => { 7 | console.log(value); 8 | }; 9 | 10 | const main = fn => { 11 | return () => log(fn()); 12 | }; 13 | 14 | const logName = main(getName); 15 | 16 | logName(); 17 | })(); 18 | -------------------------------------------------------------------------------- /historia-conceitos/hoisting-funcoes.js: -------------------------------------------------------------------------------- 1 | function fn() { 2 | log("Hoisting de função"); 3 | 4 | function log(value) { 5 | console.log(value); 6 | } 7 | } 8 | 9 | fn(); 10 | 11 | /** 12 | function fn() { 13 | function log(value) { 14 | console.log(value); 15 | } 16 | 17 | log('Hoisting de função'); 18 | } 19 | */ 20 | -------------------------------------------------------------------------------- /historia-conceitos/hoisting-variaveis.js: -------------------------------------------------------------------------------- 1 | function fn() { 2 | console.log(text); 3 | 4 | var text = "Exemplo"; 5 | 6 | console.log(text); 7 | } 8 | 9 | fn(); 10 | 11 | /** 12 | function fn() { 13 | var text; 14 | console.log(text); 15 | 16 | text = 'Exemplo'; 17 | 18 | console.log(text); 19 | } 20 | */ 21 | -------------------------------------------------------------------------------- /historia-conceitos/imutabilidade-filter.js: -------------------------------------------------------------------------------- 1 | (() => { 2 | const students = [ 3 | { 4 | name: "Grace", 5 | grade: 7 6 | }, 7 | { 8 | name: "Paul", 9 | grade: 4 10 | }, 11 | { 12 | name: "Jennifer", 13 | grade: 10 14 | } 15 | ]; 16 | 17 | function getApprovedStudents(studentsList) { 18 | return studentsList.filter(student => student.grade >= 7); 19 | } 20 | 21 | const approvedStudents = getApprovedStudents(students); 22 | })(); 23 | -------------------------------------------------------------------------------- /historia-conceitos/imutabilidade.js: -------------------------------------------------------------------------------- 1 | (() => { 2 | const user = { 3 | name: "Guilherme", 4 | lastName: "Cabrini da Silva" 5 | }; 6 | 7 | function getUserWithFullName(user) { 8 | return { 9 | ...user, 10 | fullName: `${user.name} ${user.lastName}` 11 | }; 12 | } 13 | 14 | const userWithFullName = getUserWithFullName(user); 15 | })(); 16 | -------------------------------------------------------------------------------- /historia-conceitos/tipagem-dinamica.js: -------------------------------------------------------------------------------- 1 | var numero = 1; 2 | numero = "Um texto qualquer"; 3 | console.log(numero); 4 | -------------------------------------------------------------------------------- /historia-conceitos/tipagem-fraca.js: -------------------------------------------------------------------------------- 1 | var meu_numero = 2; 2 | 3 | var minha_string = "texto"; 4 | 5 | console.log(meu_numero + minha_string); 6 | -------------------------------------------------------------------------------- /tipos-e-variaveis/escopo/tipos-escopos.js: -------------------------------------------------------------------------------- 1 | // escopo global 2 | 3 | { 4 | // escopo de bloco 5 | } 6 | 7 | function test() { 8 | // escopo de função 9 | } 10 | -------------------------------------------------------------------------------- /tipos-e-variaveis/tipos/array.js: -------------------------------------------------------------------------------- 1 | const users = ["Guilherme", "Pedro", "Jennifer"]; 2 | 3 | const gender = { 4 | MAN: Symbol("M"), 5 | WOMAN: Symbol("W") 6 | }; 7 | 8 | const persons = [ 9 | { 10 | name: "Guilherme", 11 | age: 26, 12 | gender: gender.MAN 13 | }, 14 | { 15 | name: "Pedro", 16 | age: 43, 17 | gender: gender.MAN 18 | }, 19 | { 20 | name: "Jennifer", 21 | age: 18, 22 | gender: gender.WOMAN 23 | } 24 | ]; 25 | 26 | // Retornar a quantidade de itens de um array 27 | console.log("Itens: ", persons.length); 28 | 29 | // Verificar se é array 30 | console.log("A variável persons é um array:", Array.isArray(persons)); 31 | 32 | // Iterar os itens do array 33 | persons.forEach(person => { 34 | console.log(`Nome: ${person.name}`); 35 | }); 36 | 37 | // Filtrar array 38 | const mens = persons.filter(person => person.gender === gender.MAN); 39 | console.log("\nNova lista apenas com homens:", mens); 40 | 41 | // Retornar um novo 42 | const personsWithCourse = persons.map(person => { 43 | person.course = "Introdução ao Javascript"; 44 | return person; 45 | }); 46 | 47 | console.log("\nPessoas com a adição do course:", personsWithCourse); 48 | 49 | // Transformar um array em outro tipo 50 | const totalAge = persons.reduce((age, person) => { 51 | age += person.age; 52 | return age; 53 | }, 0); 54 | 55 | console.log("\nSoma de idade das pessoas", totalAge); 56 | 57 | // Juntando operações 58 | const totalEvenAges = persons 59 | .filter(person => person.age % 2 === 0) 60 | .reduce((age, person) => { 61 | age += person.age; 62 | return age; 63 | }, 0); 64 | console.log( 65 | "\nSoma de idades das pessoas que possuem idade par", 66 | totalEvenAges 67 | ); 68 | -------------------------------------------------------------------------------- /tipos-e-variaveis/tipos/boolean.js: -------------------------------------------------------------------------------- 1 | const isActive = true; 2 | 3 | const isAuthenticated = false; 4 | 5 | console.log("Tipo de variável:", typeof isActive); 6 | -------------------------------------------------------------------------------- /tipos-e-variaveis/tipos/functions-contexto.js: -------------------------------------------------------------------------------- 1 | (() => { 2 | this.name = "arrow function"; 3 | 4 | const getNameArrowFn = () => this.name; 5 | 6 | function getName() { 7 | return this.name; 8 | } 9 | 10 | const user = { 11 | name: "Nome no objeto de execução", 12 | getNameArrowFn, 13 | getName 14 | }; 15 | 16 | console.log(user.getNameArrowFn()); 17 | console.log(user.getName()); 18 | })(); 19 | -------------------------------------------------------------------------------- /tipos-e-variaveis/tipos/functions.js: -------------------------------------------------------------------------------- 1 | function fn() { 2 | return "Code here"; 3 | } 4 | 5 | const arrowFn = () => "Code here"; 6 | const arrowFn2 = () => { 7 | // Mais de uma expressão 8 | return "Code here"; 9 | }; 10 | 11 | // Funções também são objetos 12 | fn.prop = "Posso criar propriedades"; 13 | 14 | console.log(fn()); 15 | console.log(fn.prop); 16 | 17 | // Receber parâmetros 18 | const logValue = value => console.log(value); 19 | const logFnResult = fnParam => console.log(fnParam()); 20 | 21 | logFnResult(fn); 22 | 23 | // Receber e retornar funções 24 | const controlFnExec = fnParam => allowed => { 25 | if (allowed) { 26 | fnParam(); 27 | } 28 | }; 29 | 30 | const handleFnExecution = controlFnExec(fn); 31 | handleFnExecution(true); // Executará a função fn 32 | handleFnExecution(); // Não executará a função fn 33 | 34 | /** 35 | // controlFnExec como função 36 | function controlFnExec(fnParam) { 37 | return function(allowed) { 38 | if (allowed) { 39 | fnParam(); 40 | } 41 | }; 42 | } 43 | */ 44 | -------------------------------------------------------------------------------- /tipos-e-variaveis/tipos/null.js: -------------------------------------------------------------------------------- 1 | const nullVariable = null; 2 | 3 | console.log(typeof nullVariable); 4 | -------------------------------------------------------------------------------- /tipos-e-variaveis/tipos/number.js: -------------------------------------------------------------------------------- 1 | const myNumber = 12.4032; 2 | 3 | // Transformar número para string 4 | const numberAsString = myNumber.toString(); 5 | console.log("Número transformado em string: ", typeof numberAsString); 6 | 7 | // Retorna número com um número de casas decimais 8 | const fixedTwoDecimals = myNumber.toFixed(2); 9 | console.log("\nNúmero com duas casas decimais:", fixedTwoDecimals); 10 | 11 | // Transforma uma string em float 12 | console.log("\nString parseada para float: ", parseFloat("13.22")); 13 | 14 | // Transforma uma string em int 15 | console.log("\nString parseada para int:", parseInt("13.20")); 16 | -------------------------------------------------------------------------------- /tipos-e-variaveis/tipos/object-funcoes.js: -------------------------------------------------------------------------------- 1 | const user = { 2 | name: "Guilherme", 3 | lastName: "Cabrini da Silva" 4 | }; 5 | 6 | // Recupera as chaves do objeto 7 | console.log("Propriedades do objeto user:", Object.keys(user)); 8 | 9 | // Retorna um array de arrasy contendo [ nome_propriedade, valor_propriedade ] 10 | console.log("\nLista de propriedades e valores:", Object.entries(user)); 11 | 12 | // Mergear propriedades de objetos 13 | Object.assign(user, { fullName: "Guilherme Cabrini da Silva" }); 14 | 15 | console.log("\nAdiciona a propriedade fullName no objeto user", user); 16 | console.log( 17 | "\nRetorna um novo objeto mergeando dois ou mais objetos", 18 | Object.assign({}, user, { age: 26 }) 19 | ); 20 | 21 | // Previne todas as alterações em um objeto 22 | const newObj = { foo: "bar" }; 23 | Object.freeze(newObj); 24 | 25 | newObj.foo = "changes"; 26 | delete newObj.foo; 27 | newObj.bar = "foo"; 28 | 29 | console.log("\nVariável new Obj após as alterações:", newObj); 30 | 31 | // Permite apenas a alteração de propriedades existentes em um objeto 32 | const person = { name: "Guilherme" }; 33 | Object.seal(person); 34 | 35 | person.name = "Guilherme Cabrini"; 36 | delete person.name; 37 | person.age = 26; 38 | 39 | console.log("\nVariável person após as alterações:", person); 40 | -------------------------------------------------------------------------------- /tipos-e-variaveis/tipos/object.js: -------------------------------------------------------------------------------- 1 | let user = { 2 | name: "Guilherme" 3 | }; 4 | 5 | // Alterando a propriedade de um objeto 6 | user.name = "Outro nome 1"; 7 | user["name"] = "Outro nome 2"; 8 | 9 | const prop = "name"; 10 | user[prop] = "Outro nome 3"; 11 | 12 | // function getProp(prop) { 13 | // return user[prop]; 14 | // } 15 | 16 | // Criando uma propriedade 17 | user.lastName = "Cabrini da Silva"; 18 | 19 | // Deletando uma propriedade 20 | delete user.name; 21 | -------------------------------------------------------------------------------- /tipos-e-variaveis/tipos/string.js: -------------------------------------------------------------------------------- 1 | // Retorna o tamanho de uma string 2 | const textSize = "Texto".length; 3 | console.log(`Quantidade de letras: ${textSize}`); 4 | 5 | // Retorna um array quebrando a string por um delimitador 6 | const splittedText = "Texto".split("x"); 7 | console.log( 8 | "\nArray com as posiçòes separadas pelo delimitador: ", 9 | splittedText 10 | ); 11 | 12 | // Busca por um valor e substitui por outro 13 | const replacedText = "Texto".replace("Text", "txeT"); 14 | console.log("\nSubstituição de valor:", replacedText); 15 | 16 | // Retorna a "fatia" de um valor 17 | const lastChar = "Texto".slice(-1); 18 | console.log("\nÚltima letra de uma string:", lastChar); 19 | 20 | const allWithoutLastChar = "Texto".slice(0, -1); 21 | console.log( 22 | "\nValor da string da primeira letra menos a última:", 23 | allWithoutLastChar 24 | ); 25 | 26 | const secondToEnd = "Texto".slice(1); 27 | console.log("\nValor da string da segunda letra até a última:", secondToEnd); 28 | 29 | // Retorna N caracteres a partir de uma posição 30 | const twoCharsBeforeFirstPos = "Texto".substr(0, 2); 31 | console.log("\nAs duas primeiras letras são:", twoCharsBeforeFirstPos); 32 | -------------------------------------------------------------------------------- /tipos-e-variaveis/tipos/symbol.js: -------------------------------------------------------------------------------- 1 | const symbol1 = Symbol(); 2 | const symbol2 = Symbol(); 3 | 4 | // Symbols são únicos 5 | console.log("symbol1 é igual a symbol2", symbol1 === symbol2); 6 | 7 | // Previnir conflito entre nomes de propriedades 8 | const nameSymbol1 = Symbol("name"); 9 | const nameSymbol2 = Symbol("name"); 10 | 11 | const user = { 12 | [nameSymbol1]: "Guilherme", 13 | [nameSymbol2]: "Outro nome", 14 | lastName: "Cabrini da Silva" 15 | }; 16 | 17 | console.log(user); 18 | 19 | // Symbols criam propriedades não enumerables 20 | for (const key in user) { 21 | if (user.hasOwnProperty(key)) { 22 | console.log(`\nValor da chave ${key}: ${user[key]}`); 23 | } 24 | } 25 | 26 | console.log("Propriedades do objeto user:", Object.keys(user)); 27 | console.log("Valores das propriedades do objeto user:", Object.values(user)); 28 | 29 | // Exibir symbols de um objeto 30 | console.log( 31 | "Symbols registrados no objeto user:", 32 | Object.getOwnPropertySymbols(user) 33 | ); 34 | 35 | // Acessando todas as propriedades do objeto 36 | console.log("Todas propriedades do objeto user:", Reflect.ownKeys(user)); 37 | 38 | // Criar um enum 39 | const directions = { 40 | UP: Symbol("UP"), 41 | DOWN: Symbol("DOWN"), 42 | LEFT: Symbol("LEFT"), 43 | RIGHT: Symbol("RIGHT") 44 | }; 45 | -------------------------------------------------------------------------------- /tipos-e-variaveis/tipos/undefined.js: -------------------------------------------------------------------------------- 1 | let undefinedVariable; 2 | 3 | console.log("Tipo da variável:", typeof undefinedVariable); 4 | -------------------------------------------------------------------------------- /tipos-e-variaveis/variaveis/1-variaveis.js: -------------------------------------------------------------------------------- 1 | var nameVar = "Guilherme"; 2 | let nameLet = "Guilherme"; 3 | const nameConst = "Guilherme"; 4 | 5 | console.log(`nameVar: ${nameVar}`); 6 | console.log(`nameLet: ${nameLet}`); 7 | console.log(`nameConst: ${nameConst}`); 8 | -------------------------------------------------------------------------------- /tipos-e-variaveis/variaveis/2-1-variaveis.js: -------------------------------------------------------------------------------- 1 | var test = "example"; 2 | 3 | (() => { 4 | console.log(`Valor dentro da função "${test}"`); 5 | 6 | if (true) { 7 | var test = "example"; 8 | console.log(`Valor dentro do if "${test}"`); 9 | } 10 | 11 | console.log(`Valor após a execução do if "${test}"`); 12 | })(); 13 | -------------------------------------------------------------------------------- /tipos-e-variaveis/variaveis/2-2-variaveis.js: -------------------------------------------------------------------------------- 1 | (() => { 2 | let test = "valor função"; 3 | 4 | console.log(`Valor dentro da função "${test}"`); 5 | 6 | if (true) { 7 | let test = "valor if"; 8 | console.log(`Valor dentro do if "${test}"`); 9 | } 10 | 11 | if (true) { 12 | let test = "valor de outro if"; 13 | console.log(`Valor de outro if "${test}"`); 14 | } 15 | 16 | console.log(`Valor após a execução do if "${test}"`); 17 | })(); 18 | -------------------------------------------------------------------------------- /tipos-e-variaveis/variaveis/2-3-variaveis.js: -------------------------------------------------------------------------------- 1 | (() => { 2 | const test = "valor função"; 3 | 4 | console.log(`Valor dentro da função "${test}"`); 5 | 6 | if (true) { 7 | const test = "valor if"; 8 | console.log(`Valor dentro do if "${test}"`); 9 | } 10 | 11 | if (true) { 12 | const test = "valor de outro if"; 13 | console.log(`Valor de outro if "${test}"`); 14 | } 15 | 16 | console.log(`Valor após a execução do if "${test}"`); 17 | })(); 18 | -------------------------------------------------------------------------------- /tipos-e-variaveis/variaveis/3-variaveis.js: -------------------------------------------------------------------------------- 1 | const name = "Guilherme"; 2 | 3 | // Não podemos alterar o valor 4 | // name = "Guilherme"; 5 | 6 | const user = { 7 | name: "Guilherme" 8 | }; 9 | 10 | // Mas se for um objeto, podemos trocar suas propriedades 11 | user.name = "Outro nome"; 12 | 13 | // Não podemos fazer o objeto "apontar" para outro lugar 14 | //user = { 15 | // name: "Guilherme" 16 | //}; 17 | 18 | const persons = ["Guilherme", "Pedro", "Jennifer"]; 19 | 20 | // Podemos adicionar novos itens 21 | persons.push("João"); 22 | 23 | // Podemos remover algum item 24 | persons.shift(); 25 | 26 | // Podemos alterar diretamente 27 | persons[1] = "James"; 28 | 29 | console.log("\nArray após as alterações: ", persons); 30 | --------------------------------------------------------------------------------