Home >Web Front-end >JS Tutorial >What are the ways to call functions in javascript

What are the ways to call functions in javascript

青灯夜游
青灯夜游Original
2021-06-22 12:00:037781browse

Methods for calling functions in JavaScript: 1. Use the "Object.Function Name()" statement to call; 2. Use "Function Name().call(Caller, Parameter 1, Parameter 2, ... ..);" statement call; 3. Use the "function name().apply(caller, parameter array);" statement call.

What are the ways to call functions in javascript

The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, Dell G3 computer.

Three ways to implement functions

Named functions

		<script type="text/javascript">
		function show(name){
			document.write(name+"  hellow")
		}
		show(&#39;laoli&#39;);
		</script>

Anonymous function (recommended)

		<script type="text/javascript">
			
		var f=function(name){
			document.write(&#39;name+"  hellow")
		}
		f(&#39;laoli&#39;);
		</script>

Use the function class to construct an anonymous function

Format: new Function (('Parameter list'), ('Parameter list'), ('Function execution body'));

Note:

  • Function has a capital letter
  • Parameter lists and function execution bodies should be enclosed in quotation marks
  • End with a semicolon
		<script type="text/javascript">
			var f = new Function(&#39;name&#39;, &#39;alert(name+"你好");&#39;);
			f(&#39;laoli&#39;);
		</script>

Column: Use of named functions

		<script type="text/javascript">
			function show(){
				document.write(&#39;我是命名函数&#39;)
			}
			var f=show();
			f();//函数调用
		</script>

Variables and function access in functions

Global variables in functions can be accessed directly

Functions in functions need to be called first before they can be accessed

Column: Implement function calls within functions (calls of local functions)

		<script type="text/javascript">
		var num=&#39;laoli&#39;;
		var f=function(num){
			document.write(num+&#39;真可爱&#39;);
			
			function show(){
				document.write(&#39;他不是女人&#39;)
			}
			show();//调用show()函数
		}
		//执行函数
		f(num);
		</script>

Result: Laoli is so cute, he is not a woman

Three ways to call functions

  • Object.Function application
  • call method calls functionFunction application.(caller,parameter 1, parameter 2, .....)
  • apply method calls the function apply(caller, parameter array)
##Note: When Declare a function that is assigned to the window object by default

Column: implement three calls

		<script type="text/javascript">
			//创建命名函数
			function show(name, age) {
				document.write(name + &#39;是男人,他&#39; + age + &#39;岁&#39;);
			}
			//对象.函数应用
			window.show(&#39;小明&#39;, &#39;30&#39;); 
			//all方法调用函数 函数应用.( 调用者,参数1 ,参数2 , .....)
			show.call(window, &#39;小明&#39;, &#39;30&#39;);
			//apply方法调用函数  apply(调用者,参数数组)
			show.apply(window, [&#39;小明&#39;, &#39;30&#39;]);
		</script>

Column: in the array Function call

		<script type="text/javascript">
			//show传入两个参数 1个数组 1个函数
			function show(arr, func) {
				//func.call(window, arr);
				func.apply(window, [arr])
			}
			show([1, 2, 3, 4], function(arr) {
				for (i in arr) {
					document.write(arr[i] + &#39;<br/>&#39;)
				}
			});
		</script>

Result:

[Related recommendations:

javascript learning tutorial]

The above is the detailed content of What are the ways to call functions 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