zoukankan      html  css  js  c++  java
  • function()

    Function Declarations:

    function functionName(parameters) {
      code to be executed
    } 

    EXAMPLE:

    <!DOCTYPE html>
    <html>
    <body>
    
    <p>This example calls a function which performs a calculation, and returns the result:</p>
    
    <p id="demo"></p>
    
    <script>
    function myFunction(a, b) {
        return a * b;
    }
    document.getElementById("demo").innerHTML = myFunction(4, 3);
    </script>
    
    </body>
    </html>

    the result: 12

    The Function() Constructor:

    <!DOCTYPE html>
    <html>
    <body>
    
    <p id="demo"></p>
    
    <script>
    var myFunction = function (a, b) {return a * b}
    document.getElementById("demo").innerHTML = myFunction(4, 3);
    </script>
    
    </body>
    </html>

    the result: 12

    Self-Invoking Functions:

    <!DOCTYPE html>
    <html>
    <body>
    
    <p>Functions can be invoked automatically without being called:</p>
    
    <p id="demo"></p>
    
    <script>
    (function () {
        document.getElementById("demo").innerHTML = "Hello! I called myself";
    })();
    </script>
    
    </body>
    </html>

    the result: Hello! I called myself

    Functions are Objects!!!

    JavaScript Closures:

    <!DOCTYPE html>
    <html>
    <body>
    
    <p>Counting with a local variable.</p>
    
    <button type="button" onclick="myFunction()">Count!</button>
    
    <p id="demo">0</p>
    
    <script>
    var add = (function () {
        var counter = 0;
        return function () {return counter += 1;}
    })();
    
    function myFunction(){
        document.getElementById("demo").innerHTML = add();
    }
    </script>
    
    </body>
    </html>

    every click the botton,the number will add one.

  • 相关阅读:
    MySQL 删除有外键约束的表数据
    Python 类装饰器解析
    保持SSH连接的linux服务器不断线
    数字货币交易所常用概念
    Python f-string
    Linux sed命令
    CAS机制详解
    MySQL缓存机制
    PHP网络请求优化
    Java三大特性---继承
  • 原文地址:https://www.cnblogs.com/QQBOSS/p/9927345.html
Copyright © 2011-2022 走看看