INFM 603: Information Technology and Organizational...
Transcript of INFM 603: Information Technology and Organizational...
![Page 1: INFM 603: Information Technology and Organizational ...lintool.github.io/UMD-courses/INFM603-2012f/slides/session05.pdfUsing Functions!! Calling functions invokes the set of instructions](https://reader033.fdocuments.in/reader033/viewer/2022051917/6008e0308a0f3c5340677ac2/html5/thumbnails/1.jpg)
INFM 603: Information Technology and Organizational Context
Jimmy Lin The iSchool ���University of Maryland��� Thursday, October 4, 2012
Session 5: JavaScript – Functions and Objects
![Page 2: INFM 603: Information Technology and Organizational ...lintool.github.io/UMD-courses/INFM603-2012f/slides/session05.pdfUsing Functions!! Calling functions invokes the set of instructions](https://reader033.fdocuments.in/reader033/viewer/2022051917/6008e0308a0f3c5340677ac2/html5/thumbnails/2.jpg)
Source: The Matrix
It’ll all make sense today…
![Page 3: INFM 603: Information Technology and Organizational ...lintool.github.io/UMD-courses/INFM603-2012f/slides/session05.pdfUsing Functions!! Calling functions invokes the set of instructions](https://reader033.fdocuments.in/reader033/viewer/2022051917/6008e0308a0f3c5340677ac2/html5/thumbnails/3.jpg)
Source: Iron Chef America
Programming… is a lot like cooking!
![Page 4: INFM 603: Information Technology and Organizational ...lintool.github.io/UMD-courses/INFM603-2012f/slides/session05.pdfUsing Functions!! Calling functions invokes the set of instructions](https://reader033.fdocuments.in/reader033/viewer/2022051917/6008e0308a0f3c5340677ac2/html5/thumbnails/4.jpg)
Functions
¢ Reusable code for performing a computation
¢ A function…
l Takes in one or more parameters l Executes some code
l Optionally returns a value
![Page 5: INFM 603: Information Technology and Organizational ...lintool.github.io/UMD-courses/INFM603-2012f/slides/session05.pdfUsing Functions!! Calling functions invokes the set of instructions](https://reader033.fdocuments.in/reader033/viewer/2022051917/6008e0308a0f3c5340677ac2/html5/thumbnails/5.jpg)
Anatomy of Functions
function convertToCelsius(f) { var celsius = 5/9 * (f-32); return celsius; } function weirdAddition(a, b) { var result = a + b - 0.5; return result; }
name of the function
list of parameters
return value
![Page 6: INFM 603: Information Technology and Organizational ...lintool.github.io/UMD-courses/INFM603-2012f/slides/session05.pdfUsing Functions!! Calling functions invokes the set of instructions](https://reader033.fdocuments.in/reader033/viewer/2022051917/6008e0308a0f3c5340677ac2/html5/thumbnails/6.jpg)
Using Functions
¢ Calling functions invokes the set of instructions it represents l Arguments to the function are specified between the parens l Multiple arguments are separated by commas
c = convertToCelsius(60);
function convertToCelsius(f) { var celsius = 5/9 * (f-32); return celsius; }
argument to the function
![Page 7: INFM 603: Information Technology and Organizational ...lintool.github.io/UMD-courses/INFM603-2012f/slides/session05.pdfUsing Functions!! Calling functions invokes the set of instructions](https://reader033.fdocuments.in/reader033/viewer/2022051917/6008e0308a0f3c5340677ac2/html5/thumbnails/7.jpg)
More Examples
var r = weirdAddition(2, 4); var a = 2; var b = 3; var s = weirdAddition(a, b);
function weirdAddition(a, b) { var result = a + b - 0.5; return result; }
![Page 8: INFM 603: Information Technology and Organizational ...lintool.github.io/UMD-courses/INFM603-2012f/slides/session05.pdfUsing Functions!! Calling functions invokes the set of instructions](https://reader033.fdocuments.in/reader033/viewer/2022051917/6008e0308a0f3c5340677ac2/html5/thumbnails/8.jpg)
You’ve already been doing it!
¢ Built in functions: l prompt("enter some text", "default"); l alert("message here");
¢ Message handlers!
![Page 9: INFM 603: Information Technology and Organizational ...lintool.github.io/UMD-courses/INFM603-2012f/slides/session05.pdfUsing Functions!! Calling functions invokes the set of instructions](https://reader033.fdocuments.in/reader033/viewer/2022051917/6008e0308a0f3c5340677ac2/html5/thumbnails/9.jpg)
Cooking analogy?
![Page 10: INFM 603: Information Technology and Organizational ...lintool.github.io/UMD-courses/INFM603-2012f/slides/session05.pdfUsing Functions!! Calling functions invokes the set of instructions](https://reader033.fdocuments.in/reader033/viewer/2022051917/6008e0308a0f3c5340677ac2/html5/thumbnails/10.jpg)
Objects
¢ It’s just a collection of properties!
var fido = { name: "Fido", weight: 40, ��� breed: "Mixed", ��� loves: ["walks", "fetching balls"] };
![Page 11: INFM 603: Information Technology and Organizational ...lintool.github.io/UMD-courses/INFM603-2012f/slides/session05.pdfUsing Functions!! Calling functions invokes the set of instructions](https://reader033.fdocuments.in/reader033/viewer/2022051917/6008e0308a0f3c5340677ac2/html5/thumbnails/11.jpg)
Objects and Properties
¢ Access object properties using the “dot” notation
¢ Where have we seen this before?
var w = fido.weight; fido.breed = "Yellow Lab";
Wine demo (Part 1)
![Page 12: INFM 603: Information Technology and Organizational ...lintool.github.io/UMD-courses/INFM603-2012f/slides/session05.pdfUsing Functions!! Calling functions invokes the set of instructions](https://reader033.fdocuments.in/reader033/viewer/2022051917/6008e0308a0f3c5340677ac2/html5/thumbnails/12.jpg)
Objects: Methods
¢ It’s just a collection of properties!
¢ Objects can have functions also! (called methods)
var fido = { name: "Fido", weight: 40, ��� breed: "Mixed", ��� loves: ["walks", "fetching balls"], bark: function() { alert("Woof woof!"); } };
![Page 13: INFM 603: Information Technology and Organizational ...lintool.github.io/UMD-courses/INFM603-2012f/slides/session05.pdfUsing Functions!! Calling functions invokes the set of instructions](https://reader033.fdocuments.in/reader033/viewer/2022051917/6008e0308a0f3c5340677ac2/html5/thumbnails/13.jpg)
Calling a Method
¢ Invoke an object’s method using the dot notation:
¢ It’s just like a function call!
¢ Where have you seen this before?
¢ What’s “this”?
fido.bark();
![Page 14: INFM 603: Information Technology and Organizational ...lintool.github.io/UMD-courses/INFM603-2012f/slides/session05.pdfUsing Functions!! Calling functions invokes the set of instructions](https://reader033.fdocuments.in/reader033/viewer/2022051917/6008e0308a0f3c5340677ac2/html5/thumbnails/14.jpg)
What’s the point?
¢ Claim: every method can be rewritten as a ordinary function, and vice versa?
¢ Why have methods? What’s the advantage of functions directly to objects?
Wine demo (Part 2)
![Page 15: INFM 603: Information Technology and Organizational ...lintool.github.io/UMD-courses/INFM603-2012f/slides/session05.pdfUsing Functions!! Calling functions invokes the set of instructions](https://reader033.fdocuments.in/reader033/viewer/2022051917/6008e0308a0f3c5340677ac2/html5/thumbnails/15.jpg)
Constructor!
¢ So far, building objects is a tedious process… that’s where constructors come in:
function Dog(name, breed, weight) { this.name = name;��� this.breed = breed; this.weight = weight; this.bark = function() { if (this.weight > 25) { alert(this.name + " says Woof!"); } else {��� alert(this.name + " says Yip!"); } }; }
![Page 16: INFM 603: Information Technology and Organizational ...lintool.github.io/UMD-courses/INFM603-2012f/slides/session05.pdfUsing Functions!! Calling functions invokes the set of instructions](https://reader033.fdocuments.in/reader033/viewer/2022051917/6008e0308a0f3c5340677ac2/html5/thumbnails/16.jpg)
Using Constructors
¢ Invoke constructors using “new”:
Wine demo (Part 3)
var fido = new Dog("Fido", "Mixed", 38);���var tiny = new Dog("Tiny", "Chawalla", 8);���var clifford = new Dog("Clifford", "Bloodhound", 65); fido.bark(); tiny.bark(); clifford.bark();
![Page 17: INFM 603: Information Technology and Organizational ...lintool.github.io/UMD-courses/INFM603-2012f/slides/session05.pdfUsing Functions!! Calling functions invokes the set of instructions](https://reader033.fdocuments.in/reader033/viewer/2022051917/6008e0308a0f3c5340677ac2/html5/thumbnails/17.jpg)
Source: The Matrix
It’ll all make sense today…
![Page 18: INFM 603: Information Technology and Organizational ...lintool.github.io/UMD-courses/INFM603-2012f/slides/session05.pdfUsing Functions!! Calling functions invokes the set of instructions](https://reader033.fdocuments.in/reader033/viewer/2022051917/6008e0308a0f3c5340677ac2/html5/thumbnails/18.jpg)
Source: The Matrix
Make sense now?
![Page 19: INFM 603: Information Technology and Organizational ...lintool.github.io/UMD-courses/INFM603-2012f/slides/session05.pdfUsing Functions!! Calling functions invokes the set of instructions](https://reader033.fdocuments.in/reader033/viewer/2022051917/6008e0308a0f3c5340677ac2/html5/thumbnails/19.jpg)
Okay, you can relax now…
Source: Flickr http://www.flickr.com/photos/kitchenplan/6258898113/
![Page 20: INFM 603: Information Technology and Organizational ...lintool.github.io/UMD-courses/INFM603-2012f/slides/session05.pdfUsing Functions!! Calling functions invokes the set of instructions](https://reader033.fdocuments.in/reader033/viewer/2022051917/6008e0308a0f3c5340677ac2/html5/thumbnails/20.jpg)
Source: The Matrix
(a bit) more of this…
![Page 21: INFM 603: Information Technology and Organizational ...lintool.github.io/UMD-courses/INFM603-2012f/slides/session05.pdfUsing Functions!! Calling functions invokes the set of instructions](https://reader033.fdocuments.in/reader033/viewer/2022051917/6008e0308a0f3c5340677ac2/html5/thumbnails/21.jpg)
What happens if a function calls itself?
Source: Wikipedia
![Page 22: INFM 603: Information Technology and Organizational ...lintool.github.io/UMD-courses/INFM603-2012f/slides/session05.pdfUsing Functions!! Calling functions invokes the set of instructions](https://reader033.fdocuments.in/reader033/viewer/2022051917/6008e0308a0f3c5340677ac2/html5/thumbnails/22.jpg)
How do we find an element in a sorted list of elements?
How do we quantify the ���speed of various algorithms?
![Page 23: INFM 603: Information Technology and Organizational ...lintool.github.io/UMD-courses/INFM603-2012f/slides/session05.pdfUsing Functions!! Calling functions invokes the set of instructions](https://reader033.fdocuments.in/reader033/viewer/2022051917/6008e0308a0f3c5340677ac2/html5/thumbnails/23.jpg)
Sequential Search
Source: Wikipedia
![Page 24: INFM 603: Information Technology and Organizational ...lintool.github.io/UMD-courses/INFM603-2012f/slides/session05.pdfUsing Functions!! Calling functions invokes the set of instructions](https://reader033.fdocuments.in/reader033/viewer/2022051917/6008e0308a0f3c5340677ac2/html5/thumbnails/24.jpg)
Binary Search
Source: Wikipedia
![Page 25: INFM 603: Information Technology and Organizational ...lintool.github.io/UMD-courses/INFM603-2012f/slides/session05.pdfUsing Functions!! Calling functions invokes the set of instructions](https://reader033.fdocuments.in/reader033/viewer/2022051917/6008e0308a0f3c5340677ac2/html5/thumbnails/25.jpg)
Algorithmic Complexity
¢ Linear vs. logarithmic algorithms
¢ Matters as data sizes increase!