Home  >  Article  >  Web Front-end  >  How to define methods in javascript

How to define methods in javascript

coldplay.xixi
coldplay.xixiOriginal
2021-04-02 11:41:246666browse

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)].

How to define methods in javascript

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn