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.

  • 相关阅读:
    刚体动力学
    碰撞检测系统
    动画系统II
    动画系统
    Game Develop Books
    光照技术
    LR参数组取值操作方法
    loadrunner测试ajax框架
    ​Web(click and script) 与 Web(HTTP/HTML)协议区别
    性能测试常用的linux命令
  • 原文地址:https://www.cnblogs.com/QQBOSS/p/9927345.html
Copyright © 2011-2022 走看看