How to define methods in JavaScript: 1. Definition formula, the code is [function test(age)]; 2. Variable formula, the code is [var print=function(name)].
The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer.
How to define methods in JavaScript:
There are two ways to define methods (functions) in JavaScript:
function funname(var1,var2){要执行的具体代码}
var funname =function(参数a,参数b...){具体方法动作}
The difference between the two ways of defining functions: the first is called definitional expression, and the second is called variable expression. In practical applications, there is no difference between the two, but there is a difference in the order in the call: the definition can be defined after the call, but the variable cannot.
Examples are as follows:
1. Definition formula
<script> function test(age){ //先定义方法,再调用 console.log(age); } test(23); </script>
2. Variable formula
<script> var print=function(name){ console.log(name); } print("tom"); </script>
Related free learning recommendations: javascript(Video)
The above is the detailed content of How to define methods in javascript. For more information, please follow other related articles on the PHP Chinese website!