SlideShare a Scribd company logo
1 of 23
introducing OO
 Learning Javascript foundations


           John Hunter
         19 October 2008
what’s OO?
   Object-oriented programming
 (OOP) is a programming paradigm
that uses objects and their interactions
        to design applications
                                 source: wikipedia/Object Orientated
review
types
                   type             examples
                  String         ' Apples' quot;42quot;
primitive type




                 Number           42 3.12 -9
                 Boolean           true false
                 Object         { x: 100, y: 200 }
reference type




                  Array        [' Apples', 42, 3.12]
                 Function       function () { ... }
functions are like other types - we can
       assign them to variables

 function showMessage (msg) {
                                       declare a named function
    console.log(msg);
 }

 var showMessage = function (msg) {
                                      declare a variable and assign it
    console.log(msg);
                                         an anonymous function
 }
function showMessage (msg) {
   console.log(msg);
}

showMessage('Goodbye');

                                 create a reference to the
var showAnother = showMessage;
                                     original function
showAnother('Hello');


Goodbye
Hello
showMessage         Function




showAnother
                      call the
                       same
                     function



              showAnother('Hello');
              showMessage('Goodbye');
...now for something
 completely different
by Roadsidepictures           http://www.techlib.com/electronics/crystal.html




                      good   bad
Object-oriented programming may be seen as a
collection of cooperating objects, as opposed to a
traditional view in which a program may be seen
 as a group of tasks to compute (quot;subroutinesquot;).


Each object can be viewed as an independent little
   machine with a distinct role or responsibility.

                                         source: wikipedia/Object Orientated
radio
      1.
           volume
           frequency

           changeVolume ()
      2.
           changeTuner ()




1. properties - hold the state of an object
2. methods - provide the object with behaviour
...to the code:
the object module
var radio = {};




                  create an object literal
var radio = {

 volume: 0,

 frequency: 88.0
};




   add properties which will store the object state
var radio = {

 volume: 0,

 frequency: 88.0,


 changeVolume: function (direction) {},

 changeTuner: function (direction) {}
};




  add methods which will give the object behaviour
var radio = {
 
 volume: 0,
 
    frequency: 88.0,
 
 
    changeVolume: function (direction) {
 
    
 if (direction === 'up') this.volume += 1;
 
    
 else this.volume -= 1;
 
    },
 
    changeTuner: function (direction) {
 
      
 if (direction === 'up') this.frequency += 4;
 
    
   else this.frequency -= 4;
 
    }
 };
code the functionality that gives methods their behaviour
adjust the volume...


radio.changeVolume('up'); // volume is 1
radio.changeVolume('up'); // volume is 2
radio.changeVolume('up'); // volume is 3
radio.changeVolume('down'); // volume is 2
tune in...

radio. changeTuner('up'); // frequency is 92.0 Mhz
radio. changeTuner('up'); // frequency is 96.0 Mhz
radio. changeTuner('up'); // frequency is 100.0 Mhz
radio. changeTuner('up'); // frequency is 104.0 Mhz
radio. changeTuner('up'); // frequency is 108.0 Mhz
radio. changeTuner('down'); // frequency is 104.0 Mhz
radio
 volume
 frequency

 changeVolume ()
 changeTuner ()




models the real world object
encapsulates code in a single object
lets see some
  real code!
Review
functions, like objects, are a data type
OO approach:
  - objects have roles and interact
  - objects model the real world and are self-contained
object module:
  - an object literal containing properies and functions
  - allows you to structure code on object-oriented principles
homework!
           r own
       you
Build
            radio
     sistor
tran                    ome
                      s
                         arts
                       p
                            ed!!
                       clud
                    in
Thanks

More Related Content

What's hot

Oop07 6
Oop07 6Oop07 6
Oop07 6
schwaa
 
11. session 11 functions and objects
11. session 11   functions and objects11. session 11   functions and objects
11. session 11 functions and objects
Phúc Đỗ
 
Advanced data structures using c++ 3
Advanced data structures using c++ 3Advanced data structures using c++ 3
Advanced data structures using c++ 3
Shaili Choudhary
 

What's hot (20)

Oop07 6
Oop07 6Oop07 6
Oop07 6
 
CLASSES, STRUCTURE,UNION in C++
CLASSES, STRUCTURE,UNION in C++CLASSES, STRUCTURE,UNION in C++
CLASSES, STRUCTURE,UNION in C++
 
OO in JavaScript
OO in JavaScriptOO in JavaScript
OO in JavaScript
 
ActionScript3 collection query API proposal
ActionScript3 collection query API proposalActionScript3 collection query API proposal
ActionScript3 collection query API proposal
 
Wrapper classes
Wrapper classesWrapper classes
Wrapper classes
 
Farhaan Ahmed, BCA 2nd Year
Farhaan Ahmed, BCA 2nd YearFarhaan Ahmed, BCA 2nd Year
Farhaan Ahmed, BCA 2nd Year
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
 
Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
 
iOS Session-2
iOS Session-2iOS Session-2
iOS Session-2
 
Built in classes in java
Built in classes in javaBuilt in classes in java
Built in classes in java
 
01 objective-c session 1
01  objective-c session 101  objective-c session 1
01 objective-c session 1
 
Introduction to c ++ part -2
Introduction to c ++   part -2Introduction to c ++   part -2
Introduction to c ++ part -2
 
JAVA CONCEPTS
JAVA CONCEPTS JAVA CONCEPTS
JAVA CONCEPTS
 
11. session 11 functions and objects
11. session 11   functions and objects11. session 11   functions and objects
11. session 11 functions and objects
 
Wrapper classes
Wrapper classes Wrapper classes
Wrapper classes
 
Advanced data structures using c++ 3
Advanced data structures using c++ 3Advanced data structures using c++ 3
Advanced data structures using c++ 3
 
Lsl scripts
Lsl scriptsLsl scripts
Lsl scripts
 
Autoboxing And Unboxing In Java
Autoboxing And Unboxing In JavaAutoboxing And Unboxing In Java
Autoboxing And Unboxing In Java
 
JVM and OOPS Introduction
JVM and OOPS IntroductionJVM and OOPS Introduction
JVM and OOPS Introduction
 
JSpiders - Wrapper classes
JSpiders - Wrapper classesJSpiders - Wrapper classes
JSpiders - Wrapper classes
 

Similar to Javascript foundations: Introducing OO

javascript
javascript javascript
javascript
Kaya Ota
 
Eqela Core API and Utilities
Eqela Core API and UtilitiesEqela Core API and Utilities
Eqela Core API and Utilities
jobandesther
 

Similar to Javascript foundations: Introducing OO (20)

Class & Object - Intro
Class & Object - IntroClass & Object - Intro
Class & Object - Intro
 
JavsScript OOP
JavsScript OOPJavsScript OOP
JavsScript OOP
 
Cocoa Design Patterns in Swift
Cocoa Design Patterns in SwiftCocoa Design Patterns in Swift
Cocoa Design Patterns in Swift
 
K is for Kotlin
K is for KotlinK is for Kotlin
K is for Kotlin
 
Oop java
Oop javaOop java
Oop java
 
Introduction to new features in java 8
Introduction to new features in java 8Introduction to new features in java 8
Introduction to new features in java 8
 
Introduction to new features in java 8
Introduction to new features in java 8Introduction to new features in java 8
Introduction to new features in java 8
 
JavaScript(Es5) Interview Questions & Answers
JavaScript(Es5)  Interview Questions & AnswersJavaScript(Es5)  Interview Questions & Answers
JavaScript(Es5) Interview Questions & Answers
 
#pugMi - DDD - Value objects
#pugMi - DDD - Value objects#pugMi - DDD - Value objects
#pugMi - DDD - Value objects
 
javascript
javascript javascript
javascript
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
 
Introduction to Client-Side Javascript
Introduction to Client-Side JavascriptIntroduction to Client-Side Javascript
Introduction to Client-Side Javascript
 
Classes1
Classes1Classes1
Classes1
 
2. Design patterns. part #2
2. Design patterns. part #22. Design patterns. part #2
2. Design patterns. part #2
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshow
 
Ember.js - A JavaScript framework for creating ambitious web applications
Ember.js - A JavaScript framework for creating ambitious web applications  Ember.js - A JavaScript framework for creating ambitious web applications
Ember.js - A JavaScript framework for creating ambitious web applications
 
6976.ppt
6976.ppt6976.ppt
6976.ppt
 
Eqela Core API and Utilities
Eqela Core API and UtilitiesEqela Core API and Utilities
Eqela Core API and Utilities
 
Oojs 1.1
Oojs 1.1Oojs 1.1
Oojs 1.1
 
JavaScript for real men
JavaScript for real menJavaScript for real men
JavaScript for real men
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 

Javascript foundations: Introducing OO

  • 1. introducing OO Learning Javascript foundations John Hunter 19 October 2008
  • 2. what’s OO? Object-oriented programming (OOP) is a programming paradigm that uses objects and their interactions to design applications source: wikipedia/Object Orientated
  • 4. types type examples String ' Apples' quot;42quot; primitive type Number 42 3.12 -9 Boolean true false Object { x: 100, y: 200 } reference type Array [' Apples', 42, 3.12] Function function () { ... }
  • 5. functions are like other types - we can assign them to variables function showMessage (msg) { declare a named function console.log(msg); } var showMessage = function (msg) { declare a variable and assign it console.log(msg); an anonymous function }
  • 6. function showMessage (msg) { console.log(msg); } showMessage('Goodbye'); create a reference to the var showAnother = showMessage; original function showAnother('Hello'); Goodbye Hello
  • 7. showMessage Function showAnother call the same function showAnother('Hello'); showMessage('Goodbye');
  • 8. ...now for something completely different
  • 9. by Roadsidepictures http://www.techlib.com/electronics/crystal.html good bad
  • 10. Object-oriented programming may be seen as a collection of cooperating objects, as opposed to a traditional view in which a program may be seen as a group of tasks to compute (quot;subroutinesquot;). Each object can be viewed as an independent little machine with a distinct role or responsibility. source: wikipedia/Object Orientated
  • 11. radio 1. volume frequency changeVolume () 2. changeTuner () 1. properties - hold the state of an object 2. methods - provide the object with behaviour
  • 12. ...to the code: the object module
  • 13. var radio = {}; create an object literal
  • 14. var radio = { volume: 0, frequency: 88.0 }; add properties which will store the object state
  • 15. var radio = { volume: 0, frequency: 88.0, changeVolume: function (direction) {}, changeTuner: function (direction) {} }; add methods which will give the object behaviour
  • 16. var radio = { volume: 0, frequency: 88.0, changeVolume: function (direction) { if (direction === 'up') this.volume += 1; else this.volume -= 1; }, changeTuner: function (direction) { if (direction === 'up') this.frequency += 4; else this.frequency -= 4; } }; code the functionality that gives methods their behaviour
  • 17. adjust the volume... radio.changeVolume('up'); // volume is 1 radio.changeVolume('up'); // volume is 2 radio.changeVolume('up'); // volume is 3 radio.changeVolume('down'); // volume is 2
  • 18. tune in... radio. changeTuner('up'); // frequency is 92.0 Mhz radio. changeTuner('up'); // frequency is 96.0 Mhz radio. changeTuner('up'); // frequency is 100.0 Mhz radio. changeTuner('up'); // frequency is 104.0 Mhz radio. changeTuner('up'); // frequency is 108.0 Mhz radio. changeTuner('down'); // frequency is 104.0 Mhz
  • 19. radio volume frequency changeVolume () changeTuner () models the real world object encapsulates code in a single object
  • 20. lets see some real code!
  • 21. Review functions, like objects, are a data type OO approach: - objects have roles and interact - objects model the real world and are self-contained object module: - an object literal containing properies and functions - allows you to structure code on object-oriented principles
  • 22. homework! r own you Build radio sistor tran ome s arts p ed!! clud in