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

An implementation of new operator in JS

An implementation of new operator in JS

This slide deck explains an implementation of JS new operation with JavaScript, which aims to explain its semantics to JS newbies.

chikoski

May 01, 2017
Tweet

More Decks by chikoski

Other Decks in Technology

Transcript

  1. Object creation without "new" operation We are JavaScripters #5 |

    April. 27th, 2017 | Mozilla Tech Speaker N.Shimizu (@chikoski)
  2. Today's topic: what happens when we use new operator? DMBTT1PJOU\

    DPOTUSVDUPS Y Z \ UIJTYY UIJTZZ ^ ^ DPOTUQOFX1PJOU     2
  3. A function to create object GVODUJPOQPJOU Y Z \ SFUVSO\

    YY  ZZ  ^ ^ DPOTUB
 QPJOU    DPOTU[FSP QPJOU    4
  4. Create objects with methods' definitions GVODUJPOQPJOU Y Z \ SFUVSO\

    YY ZZ  EJTUBODFGVODUJPO QU \ DPOTUEYUIJTYQUY DPOTUEZUIJTZQUZ SFUVSO.BUIBCT EY  .BUIBCT EZ  ^ ^ 5
  5. Shared function object 7 x: 10, y: 10 x: 0,

    y:0 function.. function.. x: 0, y:0 function.. x: 10, y: 10
  6. A chunk of methods for objects created with point function

    DPOTUQPJOU.FUIPET\ EJTUBODFGVODUJPO QU \ DPOTUEYUIJTYQUY DPOTUEZUIJTZQUZ SFUVSO.BUIBCT EY  .BUIBCT EZ  ^ OPSNGVODUJPO \ SFUVSOUIJTEJTUBODF \Y Z^  ^ ^ 8
  7. Modified point function GVODUJPOQPJOU Y Z \ DPOTUPCK\YY ZZ^ GPS

    DPOTUOBNFJOQPJOU.FUIPET \ PCK<OBNF>QPJOU.FUIPET<OBNF> ^ SFUVSOPCK ^ 9
  8. Separating object creation process from its initialization GVODUJPODSFBUF JOJUJBMJ[FS BSHT

    \ DPOTUPCK\^ JOJUJBMJ[FSBQQMZ PCK  "SSBZQSPUPUZQFTMJDFBQQMZ BSHT  SFUVSOPCK ^ 10
  9. create function usage GVODUJPOQPJOU Y Z \ UIJTYY UIJTZZ GPS

    DPOTUOBNFJOQPJOU.FUIPET \ UIJT<OBNF>QPJOU.FUIPET<OBNF> ^ ^ DPOTUQDSFBUF QPJOU    DPOTU[FSPDSFBUF QPJOU    11
  10. Parameterize methods chunk GVODUJPODSFBUF JOJUJBMJ[FS NFUIPET BSHT \ DPOTUPCK\^ GPS

    DPOTUOBNFJONFUIPET \ PCK<OBNF>NFUIPET<OBNF> ^ JOJUJBMJ[FSBQQMZ PCK  "SSBZQSPUPUZQFTMJDFBQQMZ BSHT  SFUVSOPCK ^ 12
  11. Calls againt modified create function GVODUJPOQPJOU Y Z \ UIJTYY

    UIJTZZ ^ DPOTUQDSFBUF QPJOU QPJOU.FUIPET    DPOTU[FSPDSFBUF QPJOU QPJOU.FUIPET    13
  12. Gathering initializer and method chunk into 1 object DPOTU1PJOU\ JOJUJBMJ[FSGVODUJPO

    Y Z \ UIJTYY UIJTZZ ^  NFUIPET\ TOJQ ^ ^ 14
  13. Modify create function to use "object template" GVODUJPODSFBUF UFNQMBUF BSHT

    \ DPOTUPCK\^ GPS DPOTUOBNFJOUFNQMBUFNFUIPET \ PCK<OBNF>UFNQMBUFNFUIPET<OBNF> ^ UFNQMBUFJOJUJBMJ[FSBQQMZ PCK  "SSBZQSPUPUZQFTMJDFBQQMZ BSHT  SFUVSOPCK ^ 15
  14. Function is also an object, so it can have attributes

    GVODUJPO1PJOU Y Z \ UIJTYY UIJTZZ ^ 1PJOUNFUIPET\ NFUIPET ^ 16
  15. Updating create methods GVODUJPODSFBUF UFNQMBUF BSHT \ DPOTUPCK\^ GPS DPOTUOBNFJOUFNQMBUFNFUIPET

    \ PCK<OBNF>UFNQMBUFNFUIPET<OBNF> ^ UFNQMBUFBQQMZ PCK "SSBZQSPUPUZQFTMJDFBQQMZ BSHT  SFUVSOPCK ^ 17
  16. How examine a object created with given template? DPOTUQDSFBUF 1PJOU

       LJOE0G Q 1PJOU USVF LJOE0G \Y Z [^ 1PJOU GBMTF 18
  17. Naive (and incomplete) implementation of kindOf GVODUJPOLJOE0G PCK UFNQMBUF \

    GPS DPOTUOBNFJOUFNQMBUFNFUIPET \ JG PCK<OBNF>OVMM \ SFUVSOGBMTF ^ ^ SFUVSOUSVF ^ 19
  18. Add special field to objects GVODUJPODSFBUF UFNQMBUF BSHT \ DPOTUPCK\^

    GPS DPOTUOBNFJOUFNQMBUFNFUIPET \ PCK<OBNF>UFNQMBUFNFUIPET<OBNF> ^ UFNQMBUFBQQMZ PCK "SSBZQSPUPUZQFTMJDFBQQMZ BSHT  PCKUFNQMBUFUFNQMBUF SFUVSOPCK ^ 20
  19. An implementation of kindOf using special field in objects GVODUJPOLJOE0G

    PCK UFNQMBUF \ SFUVSOPCKUFNQMBUFUFNQMBUF ^ 21
  20. Now we have implemented object creation system 22 Function /

    attributes Equivalent feature in JS syntax create new Template Consturctor Template.methods Function.prototype kindOf instanceof object.template object.constructor