Skip to the content.
What Is A Function?
- Functions let you group a series of statements together to perform a specific task.
- If different parts of a script repeat the same task, you can reuse the function.
Declaring A Function
- To create a function, you give it name and then write the statements needed to achieve its task inside the curly braces.
- This is known as a function declaration.
Calling A Function
- Having declared the function, you can then execute all of the statements between its curly braces with just one line of code.
- Thins is known as calling the function.
- Sometimes a function needs specific information to perform its task.
- In such cases, when you declare the function you give it parameters.
- Inside the function, the parameters act like variables.
- 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.
Getting A Single Value Out Of A Function
- Some functions return information to the code that called them.
- For example, when they perform a calculationl, they return the result.
Getting Multiple Values Out Of A Function
- Functions can return more than one value using an array.
- For example, the function calculates the area and volume of a box.
What Is An Object?
- Objects group together a set of variables and functions to create a model of something you would recognize from the real world.
- In an object, variables and functions take on new names.
IN AN OBJECT: VARIALBLES BECOME KNOWN AS PROPERTIES
- 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.
IN AN OBJECT: FUNCTIONS BECOME KNOWN AS METHODS
- If a function is part of an object, it is called a method.
- Methods represent tasks that are associated with the object.
Adding AND Removing Properties
- Once you have created an object, you can add new properties to it.
- You do this using the dot notation.
- Ex. hotel.gym = true; hotel.pool = false
- To delete a property, you use the keyword delete, and then use dot notation to identify the property or method you want to remove from the object.
- Ex. delete.hotel.booked
This
- The keyword this is commonly used inside functions and objects.
- Where the function is declared alters what this means.
- It always refer to one object, usually the object in which the function operates.
Arrays And Objects
- Arrays are actually a special type of object.
- They hold a related set of key and value pairs, but the key for each value is its index number.
- You can combine arrays and objects to create complex data structures.
- Arrays can store a series of objects and remember their orders.
- Objects can also hold arrays as value of their properties.
What Are Built-In Objects
- Browsers come with a set of built-in objects that represent things like the browser window and the current web page shown in that window.
- These built-in objects act like a toolkit for creating interactive web pages.
- Browser Object Model
- Document Object Model
- Global JavaScript Objects