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

20170327-wejs

 20170327-wejs

Brief introduction of object oriented programming, especially encapsulation.

chikoski

March 28, 2017
Tweet

More Decks by chikoski

Other Decks in Technology

Transcript

  1. Object oriented program in JavaScript We are JavaScripters #4 |

    Mar. 27th, 2017 | Mozilla Tech Speaker N.Shimizu (@chikoski)
  2. Functions responsible to counter status management MFUDPVOUFS7BMVF GVODUJPODPVOUVQ \ DPVOUFS7BMVF

     ^ GVODUJPOJOJUJBMJ[F \ DPVOUFS7BMVF ^ GVODUJPOUP/VNCFS \ SFUVSODPVOUFS7BMVF ^ 5
  3. Typical usage of added functions JOJUJBMJ[F  DPVOUVQ  DPVOUVQ

     DPOTUOVNUP/VNCFS  DPOTPMFMPH AWJTJUPST\OVN^A  6
  4. 8      DPVOUVQ DPVOUVQ DPVOUVQ DPVOUVQ

    UP/VNCFS UP/VNCFS UP/VNCFS UP/VNCFS UP/VNCFS
  5. Showing functions' responsibility by compositing to a object MFUDPVOUFS7BMVF DPOTUDPVOUFS\

    JOJUJBMJ[FGVODUJPO \DPVOUFS7BMVF^  DPVOUVQGVODUJPO \DPVOUFS7BMVF ^  UP/VNCFS \SFUVSODPVOUFS7BMVF^ ^ 9
  6. A object can contain values as its attributes DPOTUDPVOUFS\ WBMVF

     JOJUJBMJ[FGVODUJPO \DPVOUFSWBMVF^  DPVOUVQGVODUJPO \DPVOUFSWBMVF ^  UP/VNCFS \SFUVSODPVOUFSWBMVF^ ^ 10
  7. Much easier to understand JOJUJBMJ[F  DPVOUVQ  DPVOUVQ 

    DPOTUOVNUP/VNCFS  11 DPVOUFSJOJUJBMJ[F  DPVOUFSDPVOUVQ  DPVOUFSDPVOUVQ  DPOTUOVN DPVOUFSUP/VNCFS 
  8. this: context of function calls DPOTUDPVOUFS\ WBMVF  JOJUJBMJ[FGVODUJPO \UIJTWBMVF^

     DPVOUVQGVODUJPO \UIJTWBMVF ^  UP/VNCFS \SFUVSOUIJTWBMVF^ ^ 12
  9. apply: function call with specifying its context DPVOUFSDPVOUVQ  DPVOUFSDPVOUVQ

     DPVOUFSUP/VNCFS  DPVOUFSUP/VNCFSBQQMZ DPVOUFS <>  DPVOUFSUP/VNCFSBQQMZ OVMM <>  DPVOUFSUP/VNCFSBQQMZ \WBMVF^ <>  13
  10. Return updated object itself DPOTUDPVOUFS\ WBMVF  JOJUJBMJ[FGVODUJPO \ DPVOUFSWBMVFSFUVSOUIJT^

     DPVOUVQGVODUJPO \ DPVOUFSWBMVF SFUVSOUIJT^  UP/VNCFS \SFUVSODPVOUFSWBMVFSFUVSOUIJT^ ^ 14
  11. Encapsulation: hiding objects' status implementation DPOTUDPVOUFS\ WBMVF<>  JOJUJBMJ[FGVODUJPO \

    UIJTWBMVF<> ^  DPVOUVQGVODUJPO \ UIJTWBMVFQVTI USVF  ^  UP/VNCFS \ SFUVSO UIJTWBMVFMFOHUI ^ ^ 17 DPOTUDPVOUFS\ WBMVF  JOJUJBMJ[FGVODUJPO \ UIJTWBMVF ^  DPVOUVQGVODUJPO \ UIJTWBMVF  ^  UP/VNCFS \ SFUVSOUIJTWBMVF ^ ^ DPOTUDPVOUFS\ BCDE  JOJUJBMJ[FGVODUJPO \ UIJTBCDE ^  DPVOUVQGVODUJPO \ UIJTBCDE  ^  UP/VNCFS \ SFUVSOUIJTBCDE ^ ^
  12. How do we design API ➔ Factors ◆ Responsibility ◆

    Number of objects ◆ Life time ◆ Relationship against other objects (has-a / has-many) ➔ Design pattern give you huge help 19
  13. Design pattern: best practices in designing objects ➔ Command pattern

    ➔ Factory pattern / Factory method pattern ➔ Observer-Observable pattern ➔ Decorator pattern ➔ Composite pattern ➔ Adapter pattern ➔ Singleton pattern 20
  14. Wrap up: encapsulation is key idea for OOP ➔ Encapsulation

    allow us to ◆ Hide actual implementation of state management ◆ Provide API for others ➔ Design pattern is super helpful for ◆ Universal language of object design ◆ Understanding libraries' / someones' design ➔ this: a reference to context of function call 22