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

What are the ways to call functions in javascript

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

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

Anonymous function (recommended)

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

Column: Use of named functions

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)

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 functionapply(caller, parameter array)
##Note: When Declare a function that is assigned to the window object by default

Column: implement three calls

Column: in the array Function call

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