Home > Web Front-end > JS Tutorial > body text

Two ways to call self-executing functions in js

不言
Release: 2018-08-23 15:36:06
Original
5897 people have browsed it

This article brings you two methods of calling self-executing functions in js. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Self-executing function definition:
After the self-executing function or self-calling function is declared, it will be called immediately. It can only be used once. There are two ways to write it.

Writing method one:(function)(actual parameter)

<script>
    (function (n1,n2){
        console.log("这是匿名函数的自执行的第一种写法,结果为:"+(n1+n2))
    })(10,100)//110

    (function start(n1,n2){
        console.log("这是函数声明方式的自执行的第一种写法,结果为:"+(n1+n2))
    })(10,100)//110</script>
Copy after login

Writing method two:Format: (function (actual parameter)) is as follows

<script>
    (function (n1,n2){
        console.log("这是匿名函数的自执行的第二种写法,结果为:"+(n1+n2))
    }(10,100))//110

    (function start(n1,n2){
        console.log("这是函数声明方式的自执行的第二种写法,结果为:"+(n1+n2))
    }(10,100))//110</script>
Copy after login

Generally, self-executing functions and closures are related, because generally when we use closures, the closures obtain the final value of the variable, so we need to use the self-executing function to get the value of the variable each time. Bind it to a closure so that the closure can get the value of each variable change.

Related recommendations:

Analysis summary of local objects & built-in objects & host objects in js

Number (number) in js Operation summary (code)

The above is the detailed content of Two ways to call self-executing functions in js. 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