SlideShare ist ein Scribd-Unternehmen logo
1 von 40
Downloaden Sie, um offline zu lesen
OBJECT ORIENTED
                           JAVASCRIPT


                           Rody Middelkoop - WT
Thursday, March 18, 2010
About Objects
  2

           Almost everything written in JavaScript is an object
           Objects can be though of as a collection of properties
            —much like a hash in other languages
           JavaScript doesn’t have a concept of classes like other
            object-oriented languages
           Classes are simulated using a concept called
            prototypal inheritance




                              CRIA-WT - Rody Middelkoop

Thursday, March 18, 2010
OBJECTS
Thursday, March 18, 2010
Object Literal Notation
  4

          // The old way
          var myObject = new Object();
          myObject.val = “test”;

          // Using object literal notation
          var myObject = {
            val: “test”
          };




                           CRIA-WT - Rody Middelkoop

Thursday, March 18, 2010
Object Literal Notation
  5

          // The old way
          var myArray = new Array(1, 30, “Refresh”);


          // Using object literal notation
          var myArray = [1, 30, “Refresh”];




                           CRIA-WT - Rody Middelkoop

Thursday, March 18, 2010
Anonymous Functions
  6


          // Using an anonymous function as an argument
          setTimeout( function () {
            alert( “Refresh” );
           }, 1000
          );
            
          // Using a function as a variable
          var myFunc = function () { 
            alert( “Refresh” ); 
          };
          setTimeout(myFunc, 1000);
                           CRIA-WT - Rody Middelkoop

Thursday, March 18, 2010
Binary Assignment
  7


          Set a default value only if the variable doesn’t have a value yet

          // Traditional ternary assignment
          var myVar = myVar ? myVar : 0;


          // Binary assignment
          var myVar = myVal || 0;




                                CRIA-WT - Rody Middelkoop

Thursday, March 18, 2010
INNER FUNCTIONS
Thursday, March 18, 2010
Scope: Inner Functions
  9

           Functions can be defined within one another
           Inner functions have access to the outer function’s
            variables and parameters.
               function getRandomInt(max) {
                 var randNum = Math.random() * max;

                    function ceil() {
                      return Math.ceil(randNum);
                    }
                    return ceil(); // Notice that no arguments are passed
               }


               alert(getRandomInt(5));

                                     CRIA-WT - Rody Middelkoop

Thursday, March 18, 2010
Closures
 10

              Inner functions maintain the scope they enjoyed when
               their parent function was called—even after the parent
               function has terminated.

              This allows you to effectively pass variables to functions
               that may not be called for some time.




                                  CRIA-WT - Rody Middelkoop

Thursday, March 18, 2010
Closures
 11


          function delayedAlert (message, delay) {
              setTimeout( function () {
                alert(message);
              }, delay);
          }
           
          // Display a message with a 5 second delay
          delayedAlert(“Refresh”, 5000);




                           CRIA-WT - Rody Middelkoop

Thursday, March 18, 2010
 Now let’s apply all of this information to a more
       classical view of OOP in JavaScript:
      Constructors

      Object Methods
          Public
          Private
          Privileged




                           OBJECTORIENTED PROGRAMMING
Thursday, March 18, 2010
METHODS
Thursday, March 18, 2010
Public Methods
 14

           One way to create a public method—a function that
            can be freely reference by code outside your object—
            is to attach it to the object’s prototype.
           An object’s prototype is a special property that acts as
            a base reference of your object.
           This prototype object is copied and applied to all new
            instances of your object.




                              CRIA-WT - Rody Middelkoop

Thursday, March 18, 2010
PROTOTYPE…
Thursday, March 18, 2010
Public methods
 16

          var Person = function(name) {
             this.name = name;
          };
          Person.prototype.say = function(){
             return this.name;
          };

          var rody = new Person(“Rody”)
          rody.say()
          var kris = new Person(“Kris”)
          kris.say()
                           CRIA-WT - Rody Middelkoop

Thursday, March 18, 2010
Prototype
 17

           Each function object has a property called prototype,
            which itself is an object and is initialized when function
            object is created
           Can be removed, replaced, modified dynamically at
            runtime if internally allowed
           Prototype is used to carry the shared state (data,
            method, etc.) among objects and also plays a role in
            inheritance of JavaScript
           Reference:
            http://en.wikipedia.org/wiki/Prototype_pattern

                               CRIA-WT - Rody Middelkoop

Thursday, March 18, 2010
Prototype usage
 18

           say() is a property of the prototype object
           but it behaves as if it's a property of the dude object




              can we tell the difference?




                                 CRIA-WT - Rody Middelkoop

Thursday, March 18, 2010
Own properties vs. prototype’s
 19




          >>> dude.hasOwnProperty('name');
          true
          >>> dude.hasOwnProperty('say');
          false




                           CRIA-WT - Rody Middelkoop

Thursday, March 18, 2010
Private Methods
 20

              Private methods are functions that are only accessible
               to methods inside your object and cannot be accessed
               by external code.




                                 CRIA-WT - Rody Middelkoop

Thursday, March 18, 2010
Private Methods
 21

            var User = function (name) {
               this.name = name;
                  function welcome () {
                    alert( “Welcome back, ” + name + “.”);
                  }
                  welcome();
                }

            var me = new User( “Bob” );  
            me.welcome();


                               CRIA-WT - Rody Middelkoop

Thursday, March 18, 2010
“Privileged” Methods
 22

           The term privileged method was coined by Douglas
            Crockford. It is not a formal construct, but rather a
            technique.
           Privileged methods essentially have one foot in the
            door:
               Then can access private methods and values within the
                object
               They are also publicly accessible




                                  CRIA-WT - Rody Middelkoop

Thursday, March 18, 2010
“Privileged” Methods
 23


           var User = function (name, age) {
              var year = (new Date()).getFullYear() 
                           – age;
              this.getYearBorn = function () {
                return year;
              };
           };

           var me = new User( “Bob”, 28 );
           alert(me.getYearBorn());


                           CRIA-WT - Rody Middelkoop

Thursday, March 18, 2010
CONSTRUCTORS
Thursday, March 18, 2010
Constructor functions
 25

          var Person = function(name) {
             this.name = name;
             this.getName = function() {
                return this.name;
             };
          };

          var rody = new Person(“Rody”);
          alert(rody.getName())

                           CRIA-WT - Rody Middelkoop

Thursday, March 18, 2010
A naming convention
 26

           MyConstructor
           myFunction




                            CRIA-WT - Rody Middelkoop

Thursday, March 18, 2010
INHERITANCE
Thursday, March 18, 2010
Parent constructor
 28

          function NormalObject() {
            this.name = 'normal';
            this.getName = function() {
             return this.name;
            };
          }




                           CRIA-WT - Rody Middelkoop

Thursday, March 18, 2010
Child constructor
 29

          function PreciousObject(){
            this.shiny = true;
            this.round = true;
          }




                           CRIA-WT - Rody Middelkoop

Thursday, March 18, 2010
The inheritance
 30

          PreciousObject.prototype =
              new NormalObject();




                           CRIA-WT - Rody Middelkoop

Thursday, March 18, 2010
A child object
 31

          var crystal_ball =
             new PreciousObject();
          crystal_ball.name = 'Crystal';

          crystal_ball.round; // true
          crystal_ball.getName(); // "Crystal"




                           CRIA-WT - Rody Middelkoop

Thursday, March 18, 2010
Calling the superclass’ constructor
 32




                           CRIA-WT - Rody Middelkoop

Thursday, March 18, 2010
INHERITANCE BY COPYING
Thursday, March 18, 2010
Two objects
 34

          var shiny = {
             shiny: true,
             round: true
          };
          var normal = {
             name: 'name me',
             getName: function() {
               return this.name;
             }
          };



                           CRIA-WT - Rody Middelkoop

Thursday, March 18, 2010
extend()
 35

          function extend(parent, child) {
            for (var i in parent) {
              child[i] = parent[i];
            }
          }




                           CRIA-WT - Rody Middelkoop

Thursday, March 18, 2010
Inheritance
 36

          extend(normal, shiny);
          shiny.getName(); // "name me"




                           CRIA-WT - Rody Middelkoop

Thursday, March 18, 2010
Grand Finale
 37

              Using Scope, Closures, Contexts, and what we’ve
               discussed about OOP, we can dynamically generate
               classes based on information supplied to the
               constructor.




                               CRIA-WT - Rody Middelkoop

Thursday, March 18, 2010
Grand Finale
 38


          function User (properties) {
             for ( var i in properties ) { function () {
               this[“get” + i] = function () {
                  return properties[i];
               };
               this[“set” + i] = function (val) {
                  properties[i] = val;
               };
                })(); }}

          var me = new User( {
             name: “Bob”,
             age: 28
          });             CRIA-WT - Rody Middelkoop

Thursday, March 18, 2010
Grand Finale
 39

          // …continued

          alert( me.name == null );
          alert( me.getname() == “Bob” );
          me.setage(21);
          alert( me.getage() == 21 );




                           CRIA-WT - Rody Middelkoop

Thursday, March 18, 2010
References and a big thank you
 40

           Pro JavaScript Techniques, by John Resig
           Douglas Crockford’s
            YUI Theater Presentations
            http://developer.yahoo.com/yui/theater
           Beginning Object Oriented JavaScript, Stoyan
            Stefanov, http://www.slideshare.net/stoyan/beginning-
            objectoriented-javascript-presentation
           Advanced Object-Oriented JavaScript, Ecker, http://
            www.slideshare.net/ecker/advanced-objectoriented-
            javascript

                             CRIA-WT - Rody Middelkoop

Thursday, March 18, 2010

Weitere ähnliche Inhalte

Andere mochten auch

Exposición en la guari
Exposición en la guariExposición en la guari
Exposición en la guariisabelri
 
Aprendemos a hacer queso
Aprendemos a hacer quesoAprendemos a hacer queso
Aprendemos a hacer quesoisabelri
 
RqueR 13 febrero 2017
RqueR 13 febrero 2017RqueR 13 febrero 2017
RqueR 13 febrero 2017isabelri
 
Graduación 2º bachillerato 2016
Graduación 2º bachillerato 2016Graduación 2º bachillerato 2016
Graduación 2º bachillerato 2016isabelri
 
Exposición de plantas medicinales
Exposición de plantas medicinalesExposición de plantas medicinales
Exposición de plantas medicinalesisabelri
 
Festival de 4º eso album fotos
Festival de 4º eso album fotosFestival de 4º eso album fotos
Festival de 4º eso album fotosisabelri
 
Entrega de publicaciones 1marzo17
Entrega de publicaciones 1marzo17Entrega de publicaciones 1marzo17
Entrega de publicaciones 1marzo17isabelri
 

Andere mochten auch (7)

Exposición en la guari
Exposición en la guariExposición en la guari
Exposición en la guari
 
Aprendemos a hacer queso
Aprendemos a hacer quesoAprendemos a hacer queso
Aprendemos a hacer queso
 
RqueR 13 febrero 2017
RqueR 13 febrero 2017RqueR 13 febrero 2017
RqueR 13 febrero 2017
 
Graduación 2º bachillerato 2016
Graduación 2º bachillerato 2016Graduación 2º bachillerato 2016
Graduación 2º bachillerato 2016
 
Exposición de plantas medicinales
Exposición de plantas medicinalesExposición de plantas medicinales
Exposición de plantas medicinales
 
Festival de 4º eso album fotos
Festival de 4º eso album fotosFestival de 4º eso album fotos
Festival de 4º eso album fotos
 
Entrega de publicaciones 1marzo17
Entrega de publicaciones 1marzo17Entrega de publicaciones 1marzo17
Entrega de publicaciones 1marzo17
 

Ähnlich wie Object Oriented JavaScript

Ähnlich wie Object Oriented JavaScript (7)

Rails in the enterprise
Rails in the enterpriseRails in the enterprise
Rails in the enterprise
 
IAT334-Lec06-OOTutorial.pptx
IAT334-Lec06-OOTutorial.pptxIAT334-Lec06-OOTutorial.pptx
IAT334-Lec06-OOTutorial.pptx
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
C++ oop
C++ oopC++ oop
C++ oop
 
Unit 2
Unit 2Unit 2
Unit 2
 

Mehr von Rody Middelkoop

Integration and Unit Testing in Java using Test Doubles like mocks and stubs
Integration and Unit Testing in Java using Test Doubles like mocks and stubsIntegration and Unit Testing in Java using Test Doubles like mocks and stubs
Integration and Unit Testing in Java using Test Doubles like mocks and stubsRody Middelkoop
 
An agile mindset in education
An agile mindset in education An agile mindset in education
An agile mindset in education Rody Middelkoop
 
Eduscrum presentatie Scrum event 2016: Scrum als onderwijsaanpak
Eduscrum presentatie Scrum event 2016: Scrum als onderwijsaanpakEduscrum presentatie Scrum event 2016: Scrum als onderwijsaanpak
Eduscrum presentatie Scrum event 2016: Scrum als onderwijsaanpakRody Middelkoop
 
Pecha Kucha eduScrum Agile Education
Pecha Kucha eduScrum Agile EducationPecha Kucha eduScrum Agile Education
Pecha Kucha eduScrum Agile EducationRody Middelkoop
 
Unit Testing Android Applications
Unit Testing Android ApplicationsUnit Testing Android Applications
Unit Testing Android ApplicationsRody Middelkoop
 
Softwarekwaliteit in een ontwikkelstraat
Softwarekwaliteit in een ontwikkelstraatSoftwarekwaliteit in een ontwikkelstraat
Softwarekwaliteit in een ontwikkelstraatRody Middelkoop
 
JavaScript on the server - Node.js
JavaScript on the server - Node.jsJavaScript on the server - Node.js
JavaScript on the server - Node.jsRody Middelkoop
 
DDOA = Software Craftmanship
DDOA = Software CraftmanshipDDOA = Software Craftmanship
DDOA = Software CraftmanshipRody Middelkoop
 
Back to the Future: Onderwijs van 1991 tot 2031
Back to the Future: Onderwijs van 1991 tot 2031Back to the Future: Onderwijs van 1991 tot 2031
Back to the Future: Onderwijs van 1991 tot 2031Rody Middelkoop
 
Scrum implemented in an educational context
Scrum implemented in an educational contextScrum implemented in an educational context
Scrum implemented in an educational contextRody Middelkoop
 
Pragmatic Model Driven Development In Java Using Smart Use Cases
Pragmatic Model Driven Development In Java Using Smart Use CasesPragmatic Model Driven Development In Java Using Smart Use Cases
Pragmatic Model Driven Development In Java Using Smart Use CasesRody Middelkoop
 
Scrum in informaticaonderwijs
Scrum in informaticaonderwijsScrum in informaticaonderwijs
Scrum in informaticaonderwijsRody Middelkoop
 
Saas: Software AND Service
Saas: Software AND ServiceSaas: Software AND Service
Saas: Software AND ServiceRody Middelkoop
 
Service Analysis And Design
Service Analysis And DesignService Analysis And Design
Service Analysis And DesignRody Middelkoop
 
Contract First Modeling Services Using Uml
Contract First Modeling Services Using UmlContract First Modeling Services Using Uml
Contract First Modeling Services Using UmlRody Middelkoop
 

Mehr von Rody Middelkoop (19)

Integration and Unit Testing in Java using Test Doubles like mocks and stubs
Integration and Unit Testing in Java using Test Doubles like mocks and stubsIntegration and Unit Testing in Java using Test Doubles like mocks and stubs
Integration and Unit Testing in Java using Test Doubles like mocks and stubs
 
An agile mindset in education
An agile mindset in education An agile mindset in education
An agile mindset in education
 
Themalunch scrum
Themalunch scrumThemalunch scrum
Themalunch scrum
 
Eduscrum presentatie Scrum event 2016: Scrum als onderwijsaanpak
Eduscrum presentatie Scrum event 2016: Scrum als onderwijsaanpakEduscrum presentatie Scrum event 2016: Scrum als onderwijsaanpak
Eduscrum presentatie Scrum event 2016: Scrum als onderwijsaanpak
 
Pecha Kucha eduScrum Agile Education
Pecha Kucha eduScrum Agile EducationPecha Kucha eduScrum Agile Education
Pecha Kucha eduScrum Agile Education
 
Unit Testing Android Applications
Unit Testing Android ApplicationsUnit Testing Android Applications
Unit Testing Android Applications
 
Software Process Models
Software Process ModelsSoftware Process Models
Software Process Models
 
Softwarekwaliteit in een ontwikkelstraat
Softwarekwaliteit in een ontwikkelstraatSoftwarekwaliteit in een ontwikkelstraat
Softwarekwaliteit in een ontwikkelstraat
 
JavaScript on the server - Node.js
JavaScript on the server - Node.jsJavaScript on the server - Node.js
JavaScript on the server - Node.js
 
DDOA = Software Craftmanship
DDOA = Software CraftmanshipDDOA = Software Craftmanship
DDOA = Software Craftmanship
 
Back to the Future: Onderwijs van 1991 tot 2031
Back to the Future: Onderwijs van 1991 tot 2031Back to the Future: Onderwijs van 1991 tot 2031
Back to the Future: Onderwijs van 1991 tot 2031
 
Scrum implemented in an educational context
Scrum implemented in an educational contextScrum implemented in an educational context
Scrum implemented in an educational context
 
Ajax And JSON
Ajax And JSONAjax And JSON
Ajax And JSON
 
Pragmatic Model Driven Development In Java Using Smart Use Cases
Pragmatic Model Driven Development In Java Using Smart Use CasesPragmatic Model Driven Development In Java Using Smart Use Cases
Pragmatic Model Driven Development In Java Using Smart Use Cases
 
Scrum in informaticaonderwijs
Scrum in informaticaonderwijsScrum in informaticaonderwijs
Scrum in informaticaonderwijs
 
Saas: Software AND Service
Saas: Software AND ServiceSaas: Software AND Service
Saas: Software AND Service
 
Service Analysis And Design
Service Analysis And DesignService Analysis And Design
Service Analysis And Design
 
ORM JPA
ORM JPAORM JPA
ORM JPA
 
Contract First Modeling Services Using Uml
Contract First Modeling Services Using UmlContract First Modeling Services Using Uml
Contract First Modeling Services Using Uml
 

Kürzlich hochgeladen

Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 

Kürzlich hochgeladen (20)

Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 

Object Oriented JavaScript

  • 1. OBJECT ORIENTED JAVASCRIPT Rody Middelkoop - WT Thursday, March 18, 2010
  • 2. About Objects 2  Almost everything written in JavaScript is an object  Objects can be though of as a collection of properties —much like a hash in other languages  JavaScript doesn’t have a concept of classes like other object-oriented languages  Classes are simulated using a concept called prototypal inheritance CRIA-WT - Rody Middelkoop Thursday, March 18, 2010
  • 4. Object Literal Notation 4 // The old way var myObject = new Object(); myObject.val = “test”; // Using object literal notation var myObject = {   val: “test” }; CRIA-WT - Rody Middelkoop Thursday, March 18, 2010
  • 5. Object Literal Notation 5 // The old way var myArray = new Array(1, 30, “Refresh”); // Using object literal notation var myArray = [1, 30, “Refresh”]; CRIA-WT - Rody Middelkoop Thursday, March 18, 2010
  • 6. Anonymous Functions 6 // Using an anonymous function as an argument setTimeout( function () {  alert( “Refresh” );  }, 1000 );    // Using a function as a variable var myFunc = function () {   alert( “Refresh” );  }; setTimeout(myFunc, 1000); CRIA-WT - Rody Middelkoop Thursday, March 18, 2010
  • 7. Binary Assignment 7 Set a default value only if the variable doesn’t have a value yet // Traditional ternary assignment var myVar = myVar ? myVar : 0; // Binary assignment var myVar = myVal || 0; CRIA-WT - Rody Middelkoop Thursday, March 18, 2010
  • 9. Scope: Inner Functions 9  Functions can be defined within one another  Inner functions have access to the outer function’s variables and parameters. function getRandomInt(max) { var randNum = Math.random() * max; function ceil() { return Math.ceil(randNum); } return ceil(); // Notice that no arguments are passed } alert(getRandomInt(5)); CRIA-WT - Rody Middelkoop Thursday, March 18, 2010
  • 10. Closures 10  Inner functions maintain the scope they enjoyed when their parent function was called—even after the parent function has terminated.  This allows you to effectively pass variables to functions that may not be called for some time. CRIA-WT - Rody Middelkoop Thursday, March 18, 2010
  • 11. Closures 11 function delayedAlert (message, delay) {   setTimeout( function () {     alert(message);   }, delay); }   // Display a message with a 5 second delay delayedAlert(“Refresh”, 5000); CRIA-WT - Rody Middelkoop Thursday, March 18, 2010
  • 12.  Now let’s apply all of this information to a more classical view of OOP in JavaScript:  Constructors  Object Methods Public Private Privileged OBJECTORIENTED PROGRAMMING Thursday, March 18, 2010
  • 14. Public Methods 14  One way to create a public method—a function that can be freely reference by code outside your object— is to attach it to the object’s prototype.  An object’s prototype is a special property that acts as a base reference of your object.  This prototype object is copied and applied to all new instances of your object. CRIA-WT - Rody Middelkoop Thursday, March 18, 2010
  • 16. Public methods 16 var Person = function(name) { this.name = name; }; Person.prototype.say = function(){ return this.name; }; var rody = new Person(“Rody”) rody.say() var kris = new Person(“Kris”) kris.say() CRIA-WT - Rody Middelkoop Thursday, March 18, 2010
  • 17. Prototype 17  Each function object has a property called prototype, which itself is an object and is initialized when function object is created  Can be removed, replaced, modified dynamically at runtime if internally allowed  Prototype is used to carry the shared state (data, method, etc.) among objects and also plays a role in inheritance of JavaScript  Reference: http://en.wikipedia.org/wiki/Prototype_pattern CRIA-WT - Rody Middelkoop Thursday, March 18, 2010
  • 18. Prototype usage 18  say() is a property of the prototype object  but it behaves as if it's a property of the dude object  can we tell the difference? CRIA-WT - Rody Middelkoop Thursday, March 18, 2010
  • 19. Own properties vs. prototype’s 19 >>> dude.hasOwnProperty('name'); true >>> dude.hasOwnProperty('say'); false CRIA-WT - Rody Middelkoop Thursday, March 18, 2010
  • 20. Private Methods 20  Private methods are functions that are only accessible to methods inside your object and cannot be accessed by external code. CRIA-WT - Rody Middelkoop Thursday, March 18, 2010
  • 21. Private Methods 21 var User = function (name) {   this.name = name;   function welcome () {     alert( “Welcome back, ” + name + “.”);   }   welcome(); } var me = new User( “Bob” );   me.welcome(); CRIA-WT - Rody Middelkoop Thursday, March 18, 2010
  • 22. “Privileged” Methods 22  The term privileged method was coined by Douglas Crockford. It is not a formal construct, but rather a technique.  Privileged methods essentially have one foot in the door: Then can access private methods and values within the object They are also publicly accessible CRIA-WT - Rody Middelkoop Thursday, March 18, 2010
  • 23. “Privileged” Methods 23 var User = function (name, age) {   var year = (new Date()).getFullYear()                 – age;   this.getYearBorn = function () {     return year;   }; }; var me = new User( “Bob”, 28 ); alert(me.getYearBorn()); CRIA-WT - Rody Middelkoop Thursday, March 18, 2010
  • 25. Constructor functions 25 var Person = function(name) { this.name = name; this.getName = function() { return this.name; }; }; var rody = new Person(“Rody”); alert(rody.getName()) CRIA-WT - Rody Middelkoop Thursday, March 18, 2010
  • 26. A naming convention 26  MyConstructor  myFunction CRIA-WT - Rody Middelkoop Thursday, March 18, 2010
  • 28. Parent constructor 28 function NormalObject() { this.name = 'normal'; this.getName = function() { return this.name; }; } CRIA-WT - Rody Middelkoop Thursday, March 18, 2010
  • 29. Child constructor 29 function PreciousObject(){ this.shiny = true; this.round = true; } CRIA-WT - Rody Middelkoop Thursday, March 18, 2010
  • 30. The inheritance 30 PreciousObject.prototype = new NormalObject(); CRIA-WT - Rody Middelkoop Thursday, March 18, 2010
  • 31. A child object 31 var crystal_ball = new PreciousObject(); crystal_ball.name = 'Crystal'; crystal_ball.round; // true crystal_ball.getName(); // "Crystal" CRIA-WT - Rody Middelkoop Thursday, March 18, 2010
  • 32. Calling the superclass’ constructor 32 CRIA-WT - Rody Middelkoop Thursday, March 18, 2010
  • 34. Two objects 34 var shiny = { shiny: true, round: true }; var normal = { name: 'name me', getName: function() { return this.name; } }; CRIA-WT - Rody Middelkoop Thursday, March 18, 2010
  • 35. extend() 35 function extend(parent, child) { for (var i in parent) { child[i] = parent[i]; } } CRIA-WT - Rody Middelkoop Thursday, March 18, 2010
  • 36. Inheritance 36 extend(normal, shiny); shiny.getName(); // "name me" CRIA-WT - Rody Middelkoop Thursday, March 18, 2010
  • 37. Grand Finale 37  Using Scope, Closures, Contexts, and what we’ve discussed about OOP, we can dynamically generate classes based on information supplied to the constructor. CRIA-WT - Rody Middelkoop Thursday, March 18, 2010
  • 38. Grand Finale 38 function User (properties) {   for ( var i in properties ) { function () {     this[“get” + i] = function () {        return properties[i];     };     this[“set” + i] = function (val) {        properties[i] = val;     };   })(); }} var me = new User( {   name: “Bob”,   age: 28 }); CRIA-WT - Rody Middelkoop Thursday, March 18, 2010
  • 39. Grand Finale 39 // …continued alert( me.name == null ); alert( me.getname() == “Bob” ); me.setage(21); alert( me.getage() == 21 ); CRIA-WT - Rody Middelkoop Thursday, March 18, 2010
  • 40. References and a big thank you 40  Pro JavaScript Techniques, by John Resig  Douglas Crockford’s YUI Theater Presentations http://developer.yahoo.com/yui/theater  Beginning Object Oriented JavaScript, Stoyan Stefanov, http://www.slideshare.net/stoyan/beginning- objectoriented-javascript-presentation  Advanced Object-Oriented JavaScript, Ecker, http:// www.slideshare.net/ecker/advanced-objectoriented- javascript CRIA-WT - Rody Middelkoop Thursday, March 18, 2010