• 吴裕雄--天生自然 JAVASCRIPT开发学习:函数调用


    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>菜鸟教程(runoob.com)</title>
    </head>
    <body>
    
    <p>
    全局函数 (myFunction) 返回参数参数相乘的结果:
    </p>
    <p id="demo"></p>
    <script>
    function myFunction(a, b) {
        return a * b;
    }
    document.getElementById("demo").innerHTML = myFunction(10, 2); 
    </script>
    
    </body>
    </html>

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>菜鸟教程(runoob.com)</title>
    </head>
    <body>
    
    <p>全局函数 myFunction() 会自动成为 window 对象的方法。</p>
    <p>myFunction() 类似于 window.myFunction()。</p>
    <p id="demo"></p>
    <script>
    function myFunction(a, b) {
        return a * b;
    }
    document.getElementById("demo").innerHTML = window.myFunction(10, 2); 
    </script>
    
    </body>
    </html>

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>菜鸟教程(runoob.com)</title>
    </head>
    <body>
    
    <p>在 HTML 中 <b>this</b> 的值, 在全局函数是一个 window 对象。</p>
    <p id="demo"></p>
    <script>
    function myFunction() {
        return this;
    }
    document.getElementById("demo").innerHTML = myFunction(); 
    </script>
    
    </body>
    </html>

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>菜鸟教程(runoob.com)</title>
    </head>
    <body>
    
    <p>myObject.fullName() 返回 John Doe:</p>
    <p id="demo"></p>
    <script>
    var myObject = {
        firstName:"John",
        lastName: "Doe",
        fullName: function() {
            return this.firstName + " " + this.lastName;
        }
    }
    document.getElementById("demo").innerHTML = myObject.fullName(); 
    </script>
    
    </body>
    </html>

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>菜鸟教程(runoob.com)</title>
    </head>
    <body>
    
    <p>在一个对象方法中 ,<b>this</b> 的值是对象本身。</p>
    <p id="demo"></p>
    <script>
    var myObject = {
        firstName:"John",
        lastName: "Doe",
        fullName: function() {
            return this;
        }
    }
    document.getElementById("demo").innerHTML = myObject.fullName();
    </script>
    
    </body>
    </html>

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>菜鸟教程(runoob.com)</title>
    </head>
    <body>
    
    <p>该实例中, myFunction 是函数构造函数:</p>
    <p id="demo"></p>
    <script>
    function myFunction(arg1, arg2) {
        this.firstName = arg1;
        this.lastName  = arg2;
    }
    var x = new myFunction("John","Doe")
    document.getElementById("demo").innerHTML = x.firstName; 
    </script>
    
    </body>
    </html>

    <!DOCTYPE html>
    <html>
    <body>
    
    <p id="demo"></p>
    
    <script>
    var myObject;
    function myFunction(a, b) {
        return a * b;
    }
    myObject = myFunction.call(myObject, 10, 2);    // 返回 20
    document.getElementById("demo").innerHTML = myObject; 
    </script>
    
    </body>
    </html>

    <!DOCTYPE html>
    <html>
    <body>
    
    <p id="demo"></p>
    
    <script>
    var myObject, myArray;
    function myFunction(a, b) {
        return a * b;
    }
    myArray = [10, 2]
    myObject = myFunction.apply(myObject, myArray);      // 返回 20
    document.getElementById("demo").innerHTML = myObject; 
    </script>
    
    </body>
    </html>

  • 相关阅读:
    佛山Uber优步司机奖励政策(2月1日~2月7日)
    长沙Uber优步司机奖励政策(2月1日~2月7日)
    广州Uber优步司机奖励政策(2月1日~2月7日)
    西安Uber优步司机奖励政策(2月1日~2月7日)
    武汉Uber优步司机奖励政策(2月1日~2月7日)
    Apicloud自定义模块
    Android Studio导出jar包
    android studio 、 as 如何导入eclipse项目
    安卓 使用Gradle生成正式签名apk文件
    如何用Android studio生成正式签名的APK文件
  • 原文地址:https://www.cnblogs.com/tszr/p/10943618.html
Copyright © 2020-2023  润新知