16 object oriented programming
play

16Object-Oriented Programming CS1101S: Programming Methodology - PowerPoint PPT Presentation

JavaScripts Native Data Structures Recap: Programming Techniques for Data Representation Object-Oriented Programming 16Object-Oriented Programming CS1101S: Programming Methodology Martin Henz October 17, 2012 Generated on Wednesday 17


  1. JavaScript’s Native Data Structures Recap: Programming Techniques for Data Representation Object-Oriented Programming 16—Object-Oriented Programming CS1101S: Programming Methodology Martin Henz October 17, 2012 Generated on Wednesday 17 October, 2012, 14:14 CS1101S: Programming Methodology 16—Object-Oriented Programming

  2. JavaScript’s Native Data Structures JavaScript’s “Arrays” (aka Vectors) Recap: Programming Techniques for Data Representation Objects in JavaScript Object-Oriented Programming 1 JavaScript’s Native Data Structures JavaScript’s “Arrays” (aka Vectors) Objects in JavaScript 2 Recap: Programming Techniques for Data Representation 3 Object-Oriented Programming CS1101S: Programming Methodology 16—Object-Oriented Programming

  3. JavaScript’s Native Data Structures JavaScript’s “Arrays” (aka Vectors) Recap: Programming Techniques for Data Representation Objects in JavaScript Object-Oriented Programming The Truth About JediScript Lists // p a i r c o n s t r u c t s a p a i r using a two − element array // LOW − LEVEL FUNCTION, NOT JEDISCRIPT function p a i r ( x , xs ) { return [ x , xs ] ; } Terminology We call these things “vectors”. We are often impose limitations on ourselves when using “vectors”. This is called “array discipline”. CS1101S: Programming Methodology 16—Object-Oriented Programming

  4. JavaScript’s Native Data Structures JavaScript’s “Arrays” (aka Vectors) Recap: Programming Techniques for Data Representation Objects in JavaScript Object-Oriented Programming Vectors in JavaScript Definition Vectors in JavaScript are tables that enjoy special syntactic support. Creating an empty vector var my vector = [ ] ; // c r e a t i n g an empty ve c tor CS1101S: Programming Methodology 16—Object-Oriented Programming

  5. JavaScript’s Native Data Structures JavaScript’s “Arrays” (aka Vectors) Recap: Programming Techniques for Data Representation Objects in JavaScript Object-Oriented Programming Adding Fields Syntax for adding fields We can add fields using the following syntax: // c r e a t i n g an empty ve c tor var my vector = [ ] ; // adding a f i e l d with key 42 my vector [ 42 ] = ”some s t r i n g ” ; // a c c e s s i n g the f i e l d a l e r t ( my vector [ 42 ] ) ; CS1101S: Programming Methodology 16—Object-Oriented Programming

  6. JavaScript’s Native Data Structures JavaScript’s “Arrays” (aka Vectors) Recap: Programming Techniques for Data Representation Objects in JavaScript Object-Oriented Programming What if the key has not been added yet? Access using non-existing keys // c r e a t i n g an empty ve c tor var my vector = [ ] ; // a c c e s s i n g using non − e x i s t e n t key a l e r t ( my vector [ 88 ] ) ; // shows ” undefined ” CS1101S: Programming Methodology 16—Object-Oriented Programming

  7. JavaScript’s Native Data Structures JavaScript’s “Arrays” (aka Vectors) Recap: Programming Techniques for Data Representation Objects in JavaScript Object-Oriented Programming What can be used as key? Anything! We can use any data structure as a keys // c r e a t i n g an empty ve c tor var my vector = [ ] ; // adding a f i e l d with key ” fortytwo ” my vector [ ” fortytwo ” ] = ”some s t r i n g ” ; // a c c e s s i n g the f i e l d a l e r t ( my vector [ ” fortytwo ” ] ) ; CS1101S: Programming Methodology 16—Object-Oriented Programming

  8. JavaScript’s Native Data Structures JavaScript’s “Arrays” (aka Vectors) Recap: Programming Techniques for Data Representation Objects in JavaScript Object-Oriented Programming Array Convention for Vectors Array Convention If the keys of a vector are integers ranging from 0 to a given number, the vector is called an array . JediScript Week 10 All vectors in JediScript are arrays. JavaScript follows this convention The built-in functions on vectors assume that vectors are arrays. For example, my vector.length ignores fields that are not non-negative integers. CS1101S: Programming Methodology 16—Object-Oriented Programming

  9. JavaScript’s Native Data Structures JavaScript’s “Arrays” (aka Vectors) Recap: Programming Techniques for Data Representation Objects in JavaScript Object-Oriented Programming Back to pair function p a i r ( x , xs ) { return [ x , xs ] ; } Literal arrays JavaScript supports the creation of a literal array, without a need to list the keys. var my array = [ ” somestring ” , ” someotherstring ” ] ; . . . my array [ 0 ] . . . CS1101S: Programming Methodology 16—Object-Oriented Programming

  10. JavaScript’s Native Data Structures JavaScript’s “Arrays” (aka Vectors) Recap: Programming Techniques for Data Representation Objects in JavaScript Object-Oriented Programming What if we want keys that are not non-negative integers? JavaScript support for strings as keys JavaScript supports strings as keys in objects . Example If my object is an object that has a field with key ”my field”, you can use it like this: // f i e l d access e x p r e s s i o n . . . my object [ ” m y f i e l d ” ] . . . // f i e l d assignment statement my object [ ” m y o t h e r f i e l d ” ] = 42; CS1101S: Programming Methodology 16—Object-Oriented Programming

  11. JavaScript’s Native Data Structures JavaScript’s “Arrays” (aka Vectors) Recap: Programming Techniques for Data Representation Objects in JavaScript Object-Oriented Programming Creating Objects Syntax of creating objects An empty object is created using the syntax {} . Example Session var my object = {} ; my object [ ”a” ] = 13; a l e r t ( my object [ ”a” ] ) ; my object [ ”b” ] = 42; a l e r t ( my object [ ”b” ] ) ; a l e r t ( my object [ ”c” ] ) ; CS1101S: Programming Methodology 16—Object-Oriented Programming

  12. JavaScript’s Native Data Structures JavaScript’s “Arrays” (aka Vectors) Recap: Programming Techniques for Data Representation Objects in JavaScript Object-Oriented Programming Syntactical Support for Objects Objects will appear a lot in your programs. We need a syntax that avoids the quotation marks around the names of fields. Syntactic Convention 1 If the key of a field is a string that looks like a JavaScript identifier, you can write . . . my object . m y f i e l d . . . instead of . . . my object [ ” m y f i e l d ” ] . . . CS1101S: Programming Methodology 16—Object-Oriented Programming

  13. JavaScript’s Native Data Structures JavaScript’s “Arrays” (aka Vectors) Recap: Programming Techniques for Data Representation Objects in JavaScript Object-Oriented Programming Syntactical Support for Objects (cont’d) Syntactic Convention 2 If the key of a field is a string that looks like a JavaScript identifier, you can write my object . m y f i e l d = . . . ; instead of my object [ ” m y f i e l d ” ] = . . . ; CS1101S: Programming Methodology 16—Object-Oriented Programming

  14. JavaScript’s Native Data Structures JavaScript’s “Arrays” (aka Vectors) Recap: Programming Techniques for Data Representation Objects in JavaScript Object-Oriented Programming Syntactical Support for Objects (cont’d) Syntactic Convention 3 We can construct objects literally , using the syntax {...} , by listing the key-value pairs, separated by “:” and “,”. Example var my object = { m y f i e l d : 42 , m y o t h e r f i e l d : 88 } ; CS1101S: Programming Methodology 16—Object-Oriented Programming

  15. JavaScript’s Native Data Structures Recap: Programming Techniques for Data Representation Object-Oriented Programming 1 JavaScript’s Native Data Structures 2 Recap: Programming Techniques for Data Representation 3 Object-Oriented Programming CS1101S: Programming Methodology 16—Object-Oriented Programming

  16. JavaScript’s Native Data Structures Recap: Programming Techniques for Data Representation Object-Oriented Programming Objects as pairs function make stack () { var stack = p a i r ( ” stack ” , [ ] ) ; return stack ; } function push ( stack , x ) { s e t t a i l ( stack , p a i r ( x , t a i l ( stack ) ) ) ; } // other f u n c t i o n s var my stack = make stack ( ) ; push ( my stack , 4 ) ; CS1101S: Programming Methodology 16—Object-Oriented Programming

  17. JavaScript’s Native Data Structures Recap: Programming Techniques for Data Representation Object-Oriented Programming Objects as functions function make account ( i n i t i a l ) { var balance = i n i t i a l ; return function ( message , amount ) { i f ( message === ” withdraw ” ) { balance = balance − amount ; return balance ; } else { // message === ” d e p o s i t ” balance = balance + amount ; return balance ; } } var my account = make account (100); my account ( ” withdraw ” , 50); CS1101S: Programming Methodology 16—Object-Oriented Programming

  18. JavaScript’s Native Data Structures Recap: Programming Techniques for Data Representation Object-Oriented Programming Objects as functions, a variant function make account ( i n i t i a l ) { var balance = i n i t i a l ; return function ( message ) { i f ( message === ” withdraw ” ) { return function ( amount ) { balance = balance − amount ; return balance ; } ; } else { // message === ” d e p o s i t ” return . . . ; } ; } var my account = make account (100); ( my account ( ” withdraw ” ) ) ( 5 0 ) ; CS1101S: Programming Methodology 16—Object-Oriented Programming

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend