Slide 1

Slide 1 text

Javascript para adultos

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

• Escopo • Hoisting • Closures • Constructors + Prototypes • Link entre objetos • Como o “this" funciona? • Objetos por referência

Slide 5

Slide 5 text

Escopo • Global • Funções • IIFE (Immediately Invoked Function Expression)

Slide 6

Slide 6 text

Escopo Global

Slide 7

Slide 7 text

var globalVar = true;

Slide 8

Slide 8 text

Escopo em Funções

Slide 9

Slide 9 text

var globalVar = true; function escopo1() { var localVar = true; } escopo1(); console.log(localVar); // Reference Error

Slide 10

Slide 10 text

var globalVar = true; function escopo1() { var localVar = true; function escopo2() { var localVar = false; console.log(localVar); // false } console.log(localVar); // true }

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

function escopo() { localVar = true; } escopo(); console.log(localVar); // true

Slide 13

Slide 13 text

IIFE (immediately invoked function expression)

Slide 14

Slide 14 text

(function() { var localVar = true; } ()); console.log(localVar); // Reference Error

Slide 15

Slide 15 text

Hoisting

Slide 16

Slide 16 text

hoisting(); // true function hoisting() { return true; }

Slide 17

Slide 17 text

function hoisting() { return true; } hoisting(); // true

Slide 18

Slide 18 text

console.log(foo); // ????? var foo = true;

Slide 19

Slide 19 text

var foo; console.log(foo); // undefined foo = true;

Slide 20

Slide 20 text

hoisting(); // ???? var hoisting = function() { return true; }

Slide 21

Slide 21 text

var hoisting; hoisting(); // undefined is not a function hoisting = function() { return true; };

Slide 22

Slide 22 text

Closures

Slide 23

Slide 23 text

(function() { } ());

Slide 24

Slide 24 text

(function($) { } (jQuery));

Slide 25

Slide 25 text

$('a').on('click', function() { console.log($); }); // Click no botão // Logs: jQuery $ = undefined; // Click no botão // Logs: undefined

Slide 26

Slide 26 text

(function($) { $('a').on('click', function() { console.log($); }); } (jQuery)); $ = undefined; // Click no botão // jQuery

Slide 27

Slide 27 text

Constructors e Prototypes

Slide 28

Slide 28 text

Constructor

Slide 29

Slide 29 text

function Pessoa() { console.log('hello'); } var mauricio = new Pessoa(); // Log: Hello

Slide 30

Slide 30 text

Prototype

Slide 31

Slide 31 text

function Pessoa() {} Pessoa.prototype.falar = function(frase) { return frase; } var mauricio = new Pessoa(); mauricio.falar('Hello World'); // Hello World

Slide 32

Slide 32 text

Link entre objetos

Slide 33

Slide 33 text

Quando um objeto é criado, um link é feito entre esse objeto e o prototype do constructor

Slide 34

Slide 34 text

var lista = new Array(); // [] lista.__proto__ === Array.prototype; // true

Slide 35

Slide 35 text

var lista = new Array(1, 2, 3); lista.push(4); console.log(lista); // [1,2,3,4]

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

var lista = new Array(1, 2, 3); lista.hasOwnProperty(1); // true

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

// Array Object lista.__proto__.__proto__ === Object.prototype // true

Slide 40

Slide 40 text

Como o "this" funciona?

Slide 41

Slide 41 text

this === window; // true Contexto Global

Slide 42

Slide 42 text

function thisTest() { return this; } thisTest() === window; // true Contexto de uma função

Slide 43

Slide 43 text

var obj = { prop: 90, method: function() { (this === obj); // true return this.prop; } }; obj.method(); // 90 Método de um objeto

Slide 44

Slide 44 text

function Validator() { this.initialize(); } Validator.prototype.initialize = function() { }; var validate = new Validator(); Em uma instância

Slide 45

Slide 45 text

function Validator() { validate.initialize(); } Validator.prototype.initialize = function() { }; var validate = new Validator(); Em uma instância

Slide 46

Slide 46 text

function Pessoa(nome) { this.nome = nome; } Pessoa.prototype.falarNome = function() { return 'Meu nome é: ' + this.nome; }; var mauricio = new Pessoa('Mauricio'); mauricio.falarNome(); // 'Meu nome é: Mauricio';

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

"this" aponta para o próprio objeto instanciado, portanto tudo que é atribuído a ele não é compartilhado entre outras instancias.

Slide 49

Slide 49 text

function Pessoa(nome) { this.nome = nome; this.falarNome = function() { return 'Meu nome é: ' + this.nome; } } var mauricio = new Pessoa('Mauricio'); mauricio.falarNome(); // 'Meu nome é: Mauricio';

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

E em callbacks?

Slide 52

Slide 52 text

[1,2,3].forEach(function() { console.log(this); }); // window, window, window

Slide 53

Slide 53 text

var obj = {}; [1,2,3].forEach(function() { console.log(this); }.bind(obj)); // obj, obj, obj Bind

Slide 54

Slide 54 text

function Validator(array) { array.forEach(this.parseArray); } Validator.prototype.parseArray = function(item) { return this; // window }; var validator = new Validator([1,2,3]);

Slide 55

Slide 55 text

function Validator(array) { array.forEach(this.parseArray.bind(this)); } Validator.prototype.parseArray = function(item) { return this; // validator }; var validator = new Validator([1,2,3]);

Slide 56

Slide 56 text

function thisTest() { return this; } thisTest() === window; // true Voltando ao exemplo de função

Slide 57

Slide 57 text

Apply ou Call

Slide 58

Slide 58 text

function thisTest() { return this; } thisTest.apply({}); // {}

Slide 59

Slide 59 text

function thisTest() { } thisTest.apply({}, [1,2,3,4,5]); // equivalente thisTest(1, 2, 3, 4, 5);

Slide 60

Slide 60 text

Diferença entre apply e call

Slide 61

Slide 61 text

thisTest.apply({}, [1, 2, 3, 4, 5]); thisTest.call({}, 1, 2, 3, 4, 5);

Slide 62

Slide 62 text

Objetos por referência

Slide 63

Slide 63 text

var a = 1; var b = a; b = 3; console.log(a); // 1

Slide 64

Slide 64 text

var a = { foo: 1 }; var b = a; b.foo = 3; console.log(a.foo); // 3

Slide 65

Slide 65 text

var a = { foo: 1 }; var b = Object.create(a); b.foo = 3; console.log(a.foo); // 1 Object.create

Slide 66

Slide 66 text

• Escopo • Hoisting • Closures • Constructors + Prototypes • Link entre objetos • Como o “this" funciona? • Objetos por referência

Slide 67

Slide 67 text

Considerações

Slide 68

Slide 68 text

Mauricio Soares /mauriciosoares /omauriciosoares Dúvidas?