w3pop.com :: ÍøÂçѧԺ :: JavaScript :: JS º¯Êý
A function is a reusable code-block that will be executed by an event, or when the function is called.
º¯ÊýÊÇ¿ÉÔÙÓõĴúÂë¿é,¿ÉÒÔÔÚʼþ´¥·¢»òÊDZ»µ÷ÓÃʱÀ´Ö´ÐС£
Function
How to call a function.
ÔõÑùÀ´µ÷ÓÃÒ»¸öº¯Êý
Function with arguments
How to pass a variable to a function, and use the variable in the function.
ÔõÑùΪº¯Êý´«µÝÒ»¸ö±äÁ¿£¬²¢ÔÚº¯ÊýÖÐʹÓÃÕâ¸ö±äÁ¿
Function with arguments 2
How to pass variables to a function, and use these variables in the function.
ÔõÑùΪº¯Êý´«µÝÒ»¸ö±äÁ¿£¬²¢ÔÚº¯ÊýÖÐʹÓÃÕâ¸ö±äÁ¿2
Function that returns a value
How to let the function return a value.
ÔõÑùʹº¯Êý·µ»ØÒ»¸öÖµ
A function with arguments, that returns a value
How to let the function find the product of 2 arguments and return the result.
ÔõÑùÈú¯Êý½«´«µÝ¸øËüµÄÁ½¸öÖµ¾¹ý´¦Àíºó·´À¡³ö×îºóµÄ½á¹û
To keep the browser from executing a script as soon as the page is loaded, you can write your script as a function.
ÒªÏëÈÃä¯ÀÀÆ÷ÔÚ¼ÓÔØÍêÒ³ÃæºóÂíÉÏÖ´Ðнű¾³ÌÐò£¬Äã¿ÉÒÔ½«½Å±¾Ð´ÈëÒ»¸öº¯ÊýÄÚ¡£
A function contains some code that will be executed only by an event or by a call to that function.
º¯ÊýÄÚµÄһЩ´úÂëÖ»ÓÐÔÚij¸öʼþ´¥·¢»ò±»µ÷ÓõÄʱºò²Å»á±»Ö´ÐС£
You may call a function from anywhere within the page (or even from other pages if the function is embedded in an external .js file).
Äã¿ÉÒÔÔÚÒ³ÃæÖеÄÈκεط½µ÷Óú¯Êý£¨¿ÉÒÔÓÃǶÈëÍⲿ.jsÎļþµÄ·½Ê½ÈÃÆäËûÒ³ÃæÒ²¿ÉÒÔʹÓýű¾£©
Functions are defined at the beginning of a page, in the <head> section.
º¯ÊýÔÚÒ³ÃæµÄ¿ªÊ¼²¿·Ö¶¨Ò壬ÔÚ<head>ÇøÓò¡£
<html> <body> |
If the line: alert("Hello world!!"), in the example above had not been written within a function, it would have been executed as soon as the line was loaded. Now, the script is not executed before the user hits the button. We have added an onClick event to the button that will execute the function displaymessage() when the button is clicked.
Èç¹ûÉÏÃæÀý×ÓÀïµÄalert("Hello world!!")ÕâÐÐûÓÐдÔÚº¯ÊýÀïµÄ»°£¬Ëü¾Í»áÔÚÕâÐб»¼ÓÔØµÄʱºò¾ÍÖ´ÐС£ÏÖÔÚ£¬½Å±¾ÔÚÓû§µã»÷°´Å¥Ç°ÊDz»»áÖ´Ðеġ£ÎÒÃÇÐèÒªÌí¼ÓÒ»¸öonClickʼþÀ´Èú¯Êýdisplaymessage()ÔÚ°´Å¥µã»÷ºóÖ´ÐÐ
You will learn more about JavaScript events in the JS Events chapter.
Ä㽫ÔÚJS Events(ʼþ)ƪÕÂÖÐѧµ½¸ü¶àÓйØJSʼþµÄÄÚÈÝ
The syntax for creating a function is:
½¨Á¢º¯ÊýµÄÓï·¨ÊÇÕâÑùµÄ£º
function functionname(var1,var2,...,varX) |
var1, var2, etc are variables or values passed into the function. The { and the } defines the start and end of the function.
var1,var2µÈһЩ±äÁ¿»òÖµ¿É´«µÝ¸øº¯ÊýʹÓá£{ºÍ}¶¨ÒåÁ˺¯ÊýµÄ¿ªÊ¼Óë½áÊø¡£
Note: A function with no parameters must include the parentheses () after the function name:
×¢Ò⣺ûÓвÎÊýµÄº¯Êý±ØÐëÔÚº¯ÊýÃû³Æºó´øÉÏ()£º
function functionname() |
Note: Do not forget about the importance of capitals in JavaScript! The word function must be written in lowercase letters, otherwise a JavaScript error occurs! Also note that you must call a function with the exact same capitals as in the function name.
×¢Ò⣺Çë²»ÒªÍü¼ÇJavaScriptÖÐÊéдҪÇóµÄÖØÒªÐÔ£¡º¯Êýfunction±ØÐëʹÓÃСд×Öĸ£¬²»È»JavaScript¾Í»á³ö´í£¡»¹ÓÐÒ»µãҪעÒâµÄ£¬ÄãËùµ÷Óõĺ¯ÊýÃû±ØÐëºÍÄ㽨Á¢µÄº¯ÊýÃûÏàÒ»Ö¡£
The return statement is used to specify the value that is returned from the function.
·µ»ØÓï¾äÓÃÀ´Ö¸¶¨´Óº¯ÊýÖзµ»ØµÄÖµ
So, functions that are going to return a value must use the return statement.
ËùÒÔ£¬Òª´Óº¯ÊýÀï·µ»ØÖµ¾Í±ØÐëʹÓ÷µ»ØÓï¾ä
The function below should return the product of two numbers (a and b):
ÏÂÃæµÄº¯Êý¾Í»á·µ»ØÁ½¸öÊý×ֵij˻ý(aºÍb):
function prod(a,b) |
When you call the function above, you must pass along two parameters:
µ±Äãµ÷ÓÃÉÏÃæµÄº¯Êý,Äã±ØÐëÌṩÁ½¸ö²ÎÊý
product=prod(2,3) |
The returned value from the prod() function is 6, and it will be stored in the variable called product.
´Óº¯Êýprod()·µ»ØµÄÖµ¾ÍΪ6,Ëü»á´æ´¢ÔÚ±äÁ¿productÖÐ
ÆÀÂÛ (0)
All