JavaScript 1.2引入了函数变量的概念,这是定义函数的另一种新方法。函数字面量是定义未命名函数的表达式。
函数变量的语法与函数语句非常相似,不同之处在于它用作表达式而不是语句,并且不需要函数名。
<script type="text/javascript"> <!-- var variablename=function(Argument List) { Function Body }; //--> </script>
从语法上讲,可以在创建文字函数时指定函数名称,如下所示。
链接:https://www.learnfk.comhttps://www.learnfk.com/javascript/javascript-function-literals.html
来源:LearnFk无涯教程网
<script type="text/javascript"> <!-- var variablename=function FunctionName(Argument List) { Function Body }; //--> </script>
请尝试以下示例。它显示了函数变量的用法。
<html> <head> <script type="text/javascript"> <!-- var func=function(x,y) { return x*y }; function secondFunction() { var result; result=func(10,20); document.write ( result ); } //--> </script> </head> <body> <p>Click the following button to call the function</p> <form> <input type="button" onclick="secondFunction()" value="Call Function"> </form> <p>Use different parameters inside the function and then try...</p> </body> </html>
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)