Functions, Methods, & Objects Flashcards
Method vs. Function
A method is on an object.
A function is independent of an object.
Calling the Function
Asking the function to perform its task
Parameters
Pieces of information passed to a function
Return Value
An answer you expect a function to provide
Arguments
When you call a function that has parameters, you specify the values it should use in the parentheses that follow its name. The values are called arguments, and they can be provided as values or as variables.
Parameters vs. Arguments
Parameters
function getArea(width, height) {
return width * height;
}
When the function is declared, you can see the words width and height used. Inside the curly braces of the function, those words act like variables.
Arguments
wallWidth = 3;
wallHeight = 3;
getArea(wallWidth, wallHeight);
You can see that the getArea() function is being called and the code specifies real numbers that will be used to perform the calculation.
Function Declaration
A function declaration creates a function that you can call later in your code. In order to call the function later in your code, you must give it a name, so these are known as named functions.
function area(width, height) {
return width * height;
};
var size = area(3, 4);
Function Expression
If you put a function where the interpreter would expect to see an expression, then it is treated as an expression, and it is known as a function expression. In function expressions, the name is usually omitted. A function with no name is called an anonymous function.
var area = function(width, height) {
return width * height;
};
var size = area(3, 4)
Local Scope
When a variable is created inside a function using the var keyword, it can only be used in that function. Below, area is a local variable.
function get(width, height) {
var area = width * height;
return area;
}
var wallSize = getArea(3, 2);
document.write(wallSize);
Global Scope
If you create a variable outside of a function, then it can be used anywhere within the script. It is called a global variable and has global scope. In the example shown, wallSize is a global variable.
function get(width, height) {
var area = width * height;
return area;
}
var wallSize = getArea(3, 2);
document.write(wallSize);
Objects, Properties, & Methods
Objects group together a set of variables and functions to create a model of a something you would recognize from the real world. In an object, variables and functions take on new names.
If a variable is part of an object, it is called a property. Properties tell us about the object, such as the name of a hotel or the number of rooms it has.
If a function is part of an object, it is called a method. Methods represent tasks that are associated with the object.
Key
Like variables and named functions, properties and methods have a name and a value. In an object, that name is called a key.
Literal Notation
var hotel = {
name: “Quay”,
rooms: 40.
booked: 25,
checkAvailability: function() {
return this.rooms - this.booked;
}
};
Object = hotel
Properties = name, rooms, booked…& values
Method = checkAvailability…& values
Key = name, rooms, booked, checkAvailability
Value = “Quay”, 40…
Constructor Notation
The new keyword and the object constructor create a blank object. You can then add properties and methods to the object.
var hotel = new Object();
hotel. name = “Quay”;
hotel. rooms = 40;
hotel. booked = 25;
hotel. checkAvailability = function () {
return this. rooms - this.booked;
};
Dot Notation
var hotelName = hotel.name;
var roomsFree = hotel.checkAvailability();