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.

  • 相关阅读:
    接水问题
    几种走法
    过河卒

    计数问题
    Java和C或C++的数据类型对照表
    记一次在家办公远程公司数据库的解决方案
    java nio 笔记
    mysql绿色版安装 遇到的问题
    mysql绿色版安装
  • 原文地址:https://www.cnblogs.com/QQBOSS/p/9927345.html
Copyright © 2011-2022 走看看