Home  >  Article  >  Web Front-end  >  What are the ways to define javascript methods (functions)

What are the ways to define javascript methods (functions)

青灯夜游
青灯夜游Original
2021-04-26 15:56:264341browse

Javascript method (function) definition method: 1. Definition formula, syntax format "function funname (parameter list) {function body;}"; 2. Variable formula, syntax format "var funname=function (parameter List){function body;}".

What are the ways to define javascript methods (functions)

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Method (method) is a javascript function called through an object. In other words, methods are also functions, just special functions. When functions and objects are written together, functions become methods.

There are two ways to define methods (functions) in JavaScript:

Two types of function definitions Differences in methods: 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 of calls: definitions can be defined after calls, but variable expressions cannot. The example is as follows

1, the definition is

<script>
function test(age){     //先定义方法,再调用
    console.log(age);
}
test(23);
</script>

<script>
test(23);  
function test(age){     //先调用,再定义方法,不会出错
    console.log(age);
}
</script>

2. Variable formula

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

<script>
		print("tom");		//先调用,再定义会出错。
		var print=function(name){
			console.log(name);
		}
</script>

Function parameter list and return value:

Function parameter list: Parameters in the function parameter list in JavaScript are not allowed to have data types; the number of function parameters can be 0~255. When there are multiple parameters, the parameters are separated by commas;

Function return value: JavaScript function If the return value type part of the function is not defined, the JavaScript function determines the return value type based on the return return value statement in the function body; if there is no return return value statement, the function has no return value.

Note:

When declaring a variable inside a function, if the var keyword is ignored, the variable is a global variable, as in the following example :

##After defining var, an error will occur in the twelfth line of code

[Recommended learning:

javascript advanced tutorial]

The above is the detailed content of What are the ways to define javascript methods (functions). 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