Table of Contents
Three ways to implement functions
Variables and function access in functions
Three ways to call functions
Home Web Front-end JS Tutorial What are the ways to call functions in javascript

What are the ways to call functions in javascript

Jun 22, 2021 pm 12:00 PM
javascript function call

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

Anonymous function (recommended)

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

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

Column: Use of named functions

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

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

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

C++ function call performance tuning: impact of parameter passing and return values C++ function call performance tuning: impact of parameter passing and return values May 04, 2024 pm 12:57 PM

C++ function call performance optimization includes two aspects: parameter passing strategy and return value type optimization. In terms of parameter passing, passing values ​​is suitable for small objects and unmodifiable parameters, while passing references or pointers is suitable for large objects and modifiable parameters, and passing pointers is the fastest. In terms of return value optimization, small values ​​can be returned directly, and large objects should return references or pointers. Choosing the appropriate strategy can improve function call performance.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

How to call functions in different modules in C++? How to call functions in different modules in C++? Apr 12, 2024 pm 03:54 PM

Calling functions across modules in C++: Declare the function: Declare the function to be called in the header file of the target module. Implement function: Implement the function in the source file. Linking modules: Use a linker to link together modules containing function declarations and implementations. Call the function: Include the header file of the target module in the module that needs to be called, and then call the function.

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

C++ function call reflection technology: parameter passing and dynamic access of return values C++ function call reflection technology: parameter passing and dynamic access of return values May 05, 2024 am 09:48 AM

C++ function call reflection technology allows dynamically obtaining function parameter and return value information at runtime. Use typeid(decltype(...)) and decltype(...) expressions to obtain parameter and return value type information. Through reflection, we can dynamically call functions and select specific functions based on runtime input, enabling flexible and scalable code.

See all articles