- Object/Oriented JavaScript
- Stoyan Stefanov
- 495字
- 2021-08-13 19:25:53
What is a Function?
Functions allow you group together some code, give this code a name, and reuse it later, addressing it by name. Let's see an example:
function sum(a, b) { var c = a + b; return c; }
What are the parts that make up a function?
- The
function
statement. - The name of the function, in this case
sum
. - Expected parameters (arguments), in this case
a
andb
. A function can accept zero or more arguments, separated by commas. - A code block, also called the body of the function.
- The
return
statement. A function always returns a value. If it doesn't return value explicitly, it implicitly returns the valueundefined
.
Note that a function can only return a single value. If you need to return more values, then simply return an array that contains all of the values as elements of this array.
Calling a Function
In order to make use of a function, you need to call it. You call a function simply by using its name followed by any parameters in parentheses. "To invoke" a function is another way of saying "to call".
Let's call the function sum()
, passing two parameters and assigning the value that the function returns to the variable result
:
>>> var result = sum(1, 2); >>> result;
3
Parameters
When defining a function, you can specify what parameters the function expects to receive when it is called. A function may not require any parameters, but if it does and you forget to pass them, JavaScript will assign the value undefined
to the ones you skipped. In the next example, the function call returns NaN
because it tries to sum 1
and undefined
:
>>> sum(1)
NaN
JavaScript is not picky at all when it comes to parameters. If you pass more parameters than the function expects, the extra parameters will be silently ignored:
>>> sum(1, 2, 3, 4, 5)
3
What's more, you can create functions that are flexible about the number of parameters they accept. This is possible thanks to the arguments
array that is created automatically inside each function. Here's a function that simply returns whatever parameters are passed to it:
>>> function args() { return arguments; } >>> args();
[]
>>> args( 1, 2, 3, 4, true, 'ninja');
[1, 2, 3, 4, true, "ninja"]
By using the arguments
array you can improve the sum()
function to accept any number of parameters and add them all up.
function sumOnSteroids() { var i, res = 0; var number_of_params = arguments.length; for (i = 0; i < number_of_params; i++) { res += arguments[i]; } return res; }
If you test this function by calling it with a different number of parameters (or even no parameters at all), you can verify that it works as expected:
>>> sumOnSteroids(1, 1, 1);
3
>>> sumOnSteroids(1, 2, 3, 4);
10
>>> sumOnSteroids(1, 2, 3, 4, 4, 3, 2, 1);
20
>>> sumOnSteroids(5);
5
>>> sumOnSteroids();
0
The expression arguments.length
returns the number of parameters passed when the function was called. Don't worry if the syntax is unfamiliar, we'll examine it in detail in the next chapter. We'll also see that arguments
is technically not an array, but an array-like object.
- Excel圖表與表格實戰技巧精粹
- 邊做邊學:3ds Max 2014動畫制作案例教程
- YUI 2.8: Learning the Library
- Adobe創意大學Illustrator產品專家認證標準教材(CS6修訂版)
- 中文版After Effects CC 2018 動漫、影視特效后期合成秘技
- PostgreSQL Replication
- IBM Lotus Notes and Domino 8.5.1
- 中文版3ds Max 2016/VRay效果圖制作實戰基礎教程(全彩版)
- 電腦寫作與定制五筆(第2版)
- iOS智能手機APP界面設計實戰教程
- Photoshop CC中文版基礎教程
- NHibernate 3 Beginner's Guide
- UG NX 9.0模具設計工廠實訓
- Sage ACT! 2011 Dashboard and Report Cookbook
- 剪映:短、中、長視頻剪輯全攻略(手機版+電腦版)