Presentation is loading. Please wait.

Presentation is loading. Please wait.

David Stotts Computer Science Department UNC Chapel Hill.

Similar presentations


Presentation on theme: "David Stotts Computer Science Department UNC Chapel Hill."— Presentation transcript:

1 David Stotts Computer Science Department UNC Chapel Hill

2

3 booyah

4

5 0. data (types, simple information) 1. data storage (variables, assignment) 2. data retrieval (expressions, evaluation) 3. repetition (loops) 4. decision making (conditionals) 5. procedure abstraction (functions) 6. data abstraction (arrays) 7. objects: all-the-above, wrapped up

6

7 Declare array as a variable var grades = [ ] ; Use each part individually grades[3] = 89.3 ; grades[ i ] = grades[ i+1] ; Use the entire array as a unit return grades ;

8 We select a single item from an array collection with an integer subscript grades[4] grades[ k+3 ] Collections are more general abstractions We allow any string “name” (or “tag” ) for a single elements in a collection not just integer

9 var stats = { } ; // similar to array declaration var stats = { “min”: 27.3, “max”: 97.7, “avg”: 78.2 } stats.min = 21.4; stats.max = 99.3; alert(stats.avg); return stats;

10 function calcStats ( nums ) { var sum=0, nAvg, nMax=-1, nMin=10000; for (var i=0; i<nums.length; i++) { sum = sum + nums[i]; if (nums[i]<nMin) { nMin=nums[i]; } if (nums[i]>nMax) { nMax=nums[i]; } } nAvg = Math.floor(100*(sum/nums.length))/100; return { “min”:nMin, “max”:nMax, “avg”:nAvg }; }

11 function calcStats ( nums ) { var sum=0; var stats = { } ; // an empty collection stats.min=10000; stats.max=-1; // add fields for (var i=0; i<nums.length; i++) { sum = sum + nums[i]; if (nums[i]<stats.min) { stats.min=nums[i]; } if (nums[i]>stats.max) { stats.max=nums[i]; } } stats.avg = Math.floor(100*(sum/nums.length))/100; return stats; }

12  Object is a collection of data and functions that operate on that data  Object is like an abstraction of a program  Before we go deeper into this, we need a deeper look at functions

13  A value is something that can be assigned to a variable…  A value is something that an expression produces when it is evaluated  Simple: 5, “tarheels”, false, -17.4 x = 2+3; y = “tarheels”; done = false;  Structured: [ 2, 5, 14, 19, 27 ] grades = [ 2, 5, 14, 19, 27 ] ;

14 Functions are values too So functions can be assigned to variables function cube (x) { return x*x*x; } function myMain() { var cf = cube; alert( cf(4) ); // prints 64, or 4^3 alert( cube(4)); // same thing }

15 Functions are values too So functions can be passed as arguments function myMain() { var cf = cube; runnit(cf); runnit(cube); // passing the “name” } function cube (x) { return x*x*x; } function runnit ( fun ) { alert ( fun(5) ); }

16 Functions are values too So functions can be explicitly initialized locally function myMain() { var newFn = function ( x ) { return x*x*x; } alert(newFn(6)); // execute the new function runnit(newFn); // pass the new function “name” } function runnit ( funarg ) { alert( funarg(5) ); }

17 Another way to do local (inner) functions function myMain() { function newFn ( x ) { return x*x*x; } // var newFn = function ( x ) { return x*x*x; } alert(newFn(6)); // execute the new function runnit(newFn); // pass the new function “name” } function runnit ( funarg ) { alert(funarg(5)); }

18 You can also return a local function as the result value produced by another function function myMain() { var sq = makeFun(); alert(sq(5)); } function makeFun ( ) { var fun = function (x) { return x*x; }; alert(“making a function: “ + fun(3)); return fun; }

19 Objects are data and functions wrapped up together var student ; // just a declaration var done=false; while (!done) { student = { }; // make a new object… “allocation” student.name = prompt(“name?”); student.grade = prompt(“grade?”); course[i] = student; }

20 Objects are data and functions wrapped up together Make global function that uses no data in the object (like cube) Show it can be called independently Install it in an object inside main Show it can be called as object method

21 Objects are data and functions wrapped up together Make global function that refers to data in the object, using “this” Install it in an object inside main Show it cannot be called directly Show it can be called as obj method and uses data in obj

22 Objects are data and functions wrapped up together Make function inside object declaration that refers to data in the object, using “this” Show it can be called as obj method and uses data in obj

23 Objects are data and functions wrapped up together  Make a function that creates a new object locally  Loads up some (or all) the data fields  Creates some functions (methods) in the object  Returns the object back to the calling function  This function is a constructor ◦ it creates perhaps many object and they all are similar… ◦ same data stored, same methods to work on that data

24


Download ppt "David Stotts Computer Science Department UNC Chapel Hill."

Similar presentations


Ads by Google