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

How to define methods in javascript

coldplay.xixi
Release: 2023-01-04 09:34:12
Original
6758 people have browsed it

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){要执行的具体代码}
Copy after login
var funname =function(参数a,参数b...){具体方法动作}
Copy after login

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>
Copy after login

2. Variable formula

<script>
   var print=function(name){
       console.log(name);
   }
   print("tom");
</script>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template