Download presentation
Presentation is loading. Please wait.
Published byPriscilla Bailey Modified over 9 years ago
1
Functions, Objects, and Programming IDIA 619 Spring 2014 Bridget M. Blodgett
2
Overview Discussion and Practice Exercise
3
Functions We’ve already used some built in functions to do work for us: Math.random, alert, console.log are all functions A custom function allows you to create reusable pieces of code that do some discrete action
4
Topics Parameters, Arguments and Return Statements Built-in Functions Local and global variables Functions as values Objects Methods “this” Constructors
5
Anatomy of a Function function checkGuess(guess) { var answers = [ "red", "green", "blue"]; var index = Math.floor(Math.random() * answers.length); if (guess == answers[index]) { answer = "You're right! I was thinking of " + answers[index]; } else { answer = "Sorry, I was thinking of " + answers[index]; } return answer; }
6
Parameters vs Arguments When you define a function you will often give it several parameters – These are placeholder that tell the function the type of information it will accept When you call the function you may pass it several arguments – These are actual values of information that match the type described by the placeholders
7
Local and Global Variables Javascript will allow you to declare a new variable just about anywhere in the document But the location you choose has an effect upon what that variable can interact with If they are declared outside of any function or loop they are a global variable – Any function or code in the document can use them If they are declared inside a function they are called local variables – Only accessible to the code that is also in the function
8
Functions as Values You can assign a function to a variable just like any other type of value (like a string or a number) function addOne(num) { return num + 1; } var plusOne = addOne; var result = plusOne(1);
9
Exercise Define a function max() that takes two numbers as arguments and returns the largest of them.
10
Objects Objects are a conceptual way of thinking about coding – Their goal is to improve planning by grouping together similar properties and functions by their goals The properties of an object are simply a way of describing that object
11
var fido = { name: "Fido", weight: 40, breed: "Mixed", loves: ["walks", "fetching balls"] };
12
Using Objects You can modify or call properites of object by using the objectname.property – Like Math.random
13
This Keyword this is used to refer to the current object that you are manipulating or working under
14
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!"); } }; } var fido = new Dog("Fido", "Mixed", 38);
15
Homework 3 Start brainstorming a small project that will use all the foundational JavaScript principles we’ve covered so far, including: – Objects – Functions – Arrays – Variables – Built-in functions – Loops – If/Else statements Bring your ideas to class next week.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.