Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Understanding JavaScript - Part 2

Nigam
March 17, 2015

Understanding JavaScript - Part 2

This talk covers Hoisting, Closure and this keyword.

Ref: http://tinyurl.com/lcuwzte and http://tinyurl.com/ljsh8z7

Nigam

March 17, 2015
Tweet

More Decks by Nigam

Other Decks in Programming

Transcript

  1. S C O P E , C L O S

    U R E , T H I S , P R O T O T Y P E U N D E R S TA N D I N G J AVA S C R I P T B Y N I G A M PA T E L
  2. A B O U T N I G A M

    • Principal Developer at Hyatt • @nigam02 • nigam-patel • nigampatel
  3. R E C A P - S C O P

    E • Block Scope • Lexical Scope • Cheating Lexical • with • eval • IIFE
  4. S C O P E - L E X I

    C A L S C O P E L O C A L G L O B A L
  5. I N T H E O RY D Y N

    A M I C S C O P I N G
  6. ‘ t h i s ’ Every function, while executing,

    has a reference to its current execution context, called this To understand this we have to understand the call-site
  7. t h i s - F O U R B

    I N D I N G R U L E S 1. new() binding 2. Explicit binding 3. Implicit binding 4. Default binding
  8. t h i s - D E FA U LT

    B I N D I N G Nigam
  9. t h i s - D E FA U LT

    B I N D I N G TypeError: `this` is `undefined`
  10. t h i s - I M P L I

    C I T B I N D I N G Robb
  11. t h i s - I M P L I

    C I T B I N D I N G Tony
  12. t h i s - E X P L I

    C I T B I N D I N G
  13. t h i s - E X P L I

    C I T B I N D I N G // Nigam // Mark
  14. t h i s - H A R D B

    I N D I N G // Nigam // Nigam
  15. t h i s - H A R D B

    I N D I N G // Nigam // Nigam https://developer.mozilla.org/en-US/docs/Web/JavaScript/ Reference/Global_Objects/Function/bind
  16. t h i s - N E W B I

    N D I N G // Nigam
  17. t h i s - N E W B I

    N D I N G 1. A brand new object is created (aka, constructed) out of thin air 2. The newly constructed object is [[Prototype]]- linked 3. The newly constructed object is set as the this binding for that function call 4. Unless the function returns its own alternate object, the new-invoked function call will automatically return the newly constructed object. ref: http://tinyurl.com/ohg4gds
  18. t h i s - F O U R B

    I N D I N G R U L E S 1. new() binding 2. Explicit binding 3. Implicit binding 4. Default binding
  19. c l o s u re Remember Lexical Scope? In

    order to understand closure, you must understand lexical scope.
  20. c l o s u re What is Closure? Closure

    is when a function is able to remember and access its lexical scope even when that function is executing outside its lexical scope. ref: http://tinyurl.com/lqy4jwf
  21. c l o s u re - H o w

    c a n w e f i x t h i s
  22. c l o s u re - L o o

    p s 5 5 5 5 5
  23. c l o s u re - L o o

    p s 0 1 2 3 4
  24. c l o s u re - F i x

    i n g i t w i t h c l o s u re 1 2 3
  25. c l o s u re - Q u i

    z - W h a t i s t h e o u t p u t ?
  26. W H AT W E D I S C U

    S S • Hoisting • this • Closure