1. Way to reuse code
2. Set up series of steps @ start of page
3. "call" the steps later
There are tons of built in functions:
calculateTax();
alert();
Number();
There is a difference between parameters and arguments, although the book doesn't say this.
The simplest way to call a function: call its name
functionName();
If you make a function that is called printMessage, you need to get the information it is to use inside the parenthesis. This is called passing the argument.
printMessage('Hello!'); // Hello! is the argument
TO MAKE A FUNCTION
1. Define/Declare Function
2. Call Function (to run it)
function myFunction() { //defining
//do this
}
myFunction(); //calling the function
A better way:
When we define a function it is called a parameter.
function printMessage(myMessage) {
document.write(myMessage + "<br/>");
}
var myMessage = "Greetings!";
printMessage(myMessage);
var myOtherMessage ="Boo hoo.";
printMessage(myOtherMessage); //it doesn't have to match what is in the function above
This would return two separate lines:
Greetings!
Boo hoo. You can pass multiple things into the function.
function printHolidayMessage(userName, holidayMessage) {
document.write("Hello" + userName + ", " + holidayMessage + "<br/>");
}
var name = prompt('Please enter your name');
var myHolidayMessage = "Happy August";
printHolidayMessage(
-----------------------------
Scope
Where the variable is visible and usable.
Where that thing is created determines which one is used.
function myFunction (a, b) {
answer = a*b;
return answer; //this keyword sends the value back out of the function
}
var answer = 0;
document.write("<p>The value of answer before myFunction called: " + answer + "</p>");
myFunction(2, 2);
document.write("<p>The new value of myAnswer after function is:" + answer + "</p>");
Global vs. Local Scope
Variables created inside the function are different, they are local.
Variables born outside can go anywhere.
Local variables are visible only inside the function
Global scope are created outside, and can be seen everywhere.
If you want the variable to only be accessible to the code inside the function then you use var keyword inside the function.
Local:
var message = 'Outside the function';
function warning() {
var message = 'Inside the function';
alert(message); //'Inside the function'
}
warning();
alert(message); //'Outside the function'
Global:
var message = 'Outside the function';
function warning() {
message = 'Inside the function';
alert(message); //'Outside the function'
}
warning();
alert(message); //'Inside the function'
--------------------
Objects
Objects:
Properties - parts like nouns or adjective
Methods - things it does like verbs
Methods are functions that are specific to an object, they use a dot syntax
Like document.write
document is an object and it has a thing that it can do, write.
Each object has its own list or properties and methods.
An array has a length property.
Instance - you can have multiple version of a same kind of object.
-------------------
Math
to generate a random number - this is useful for giving you a number to use for an arrayIt generates a random number between 0 and whatever you set (num).
But it doesn't give you a new number unless you use:
Math.floor - gives you the number below (4.898 will be 4)
Math.ceil - gives you the number up (4.898 will be 5)
Math.round - rounds like regular
function chooseRandomNum(num) {
var randomNum = Math.floor(Math.random) * num );
}
No comments:
Post a Comment