JavaScript For Kids For Dummies: Chapter Twelve (Peer - Programming)(Making It Functional).

preview_player
Показать описание
Making It Functional

Functions are the building blocks of JavaScript

programs. They help you to keep from having to repeat yourself, and they make your programs neater and more flexible!
In this chapter, we use functions to create a game called
Function Junction.

Understanding Functions
Functions are programs inside of programs. Functions are great at
handling tasks that may be required multiple times by different
parts of a program. They’re also a great way to keep things
organized within your program.
Built‐in functions
Some functions are built into JavaScript, such as the methods of
arrays, strings, numbers, and other objects. You can tell that
something’s a function because it has parentheses after it.
When a function is part of an object (such as the document
object, for example) we call it a method. But it’s still a function.
Here are some of the functions that are built into JavaScript that
you’ve already used:
getElementById()
toString()
addEventListener()
indexOf()
Can you think of other functions you’ve seen?
Custom functions
In addition to the functions that are built into JavaScript, you can
also create your own, using the steps and information that we’re
about to show you!
You can’t spell function without “fun,” and you can’t write
JavaScript code without using functions. Therefore, you can’t
write JavaScript code without having fun!

If you’ve read through the previous chapters, you’ve already
seen functions in action in the demonstration programs. Here’s
an example of a function that adds a smiley face to any text you
give it:
function smileyIt(theText) {

7. Click the Run link.

alert() is another example of a built‐in JavaScript function!

Another way to define a function is by using the new Function
technique. That looks like this:
var myFunction = new Function() {
// statements go here
}
Both methods get the job done, but we recommend using the first,
more common, technique.
Giving the function a head
The first part of a function definition is called the function head.
The function head includes the function keyword, the name of the
function, and the parentheses:
function myFunction()
Filling out the function body
Next up is the function body. The function body is made up of
statements, surrounded by curly braces. For example:
{
// this is the function body
}
Calling a function
When you run the code within a function body, that’s called
calling the function. To call a function, you simply write the name
of the function, followed by parentheses. For example:
myFunction();
Defining parameters
Parameters are values that can be included between the parentheses when a function is called. To define a parameter, simply give
the parameter a name and put it between the parentheses in the
function definition. For example:
Chapter 12: Making It Functional 195
function myFunction(theText) {
}
You can define multiple parameters by separating them with
commas.
Passing arguments
When you call a function using a value between the parentheses,
it’s called passing an argument. For example:
myFunction("This is some text");
In this case, the argument is the string "This is some text".
When you’re defining a function, the values between the parentheses are called parameters. When passing values into a function,
they’re called arguments.
When you pass an argument into a function, the function automatically creates a new variable with the name of the parameter and
gives it the value of the argument that you passed.
Returning a value
When you call a function and (optionally) pass it an argument, the
function starts doing its thing. After the function completes its
task, it stops running and produces some sort of value. The value
that a function produces when it finishes running is called its
return value.
You can set what the return value is by using the return statement.
For example, the following function will always return the number
3000:
function whatsTheNumber(){
return 3000;
}
196 Part IV: Arrays and Functions
To find out or use the return value of a function, you can call the
function as part of an operation. For example, to do math with the
result of a function, you just include the function as a normal
operand (see Chapter 8), like this:
var theTotal = whatsTheNumber() + 80;
When you run this statement, a value equal to the return value
of whatsTheNumber() plus 80 (or 3080) will be assigned to
theTotal.
If you don’t specify a return value for a function, the function will
return undefined.
Knowing What Functions Are Made Of
Functions have a special vocabulary and way that they must be
written and used. To really understand functions, you need to be
able to speak their language. So, let’s look at a couple words and
take apart a function to see what’s inside!
Defining a function
When you write a function, that’s called defining it. Defining a
function makes the code inside that function available to be run.
There are a couple different ways to define a function. The way to use the keyword key.
Рекомендации по теме
visit shbcf.ru