• 吴裕雄--天生自然 JAVASCRIPT开发学习: this 关键字


    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>菜鸟教程(runoob.com)</title>
    </head>
    <body>
    
    <h2>JavaScript <b>this</b> 关键字</h2>
    
    <p>实例中,<b>this</b> 指向了 <b>person</b> 对象。</p>
    <p>因为 person 对象是 fullName 方法的所有者。</p>
    
    <p id="demo"></p>
    
    <script>
    // 创建一个对象
    var person = {
      firstName: "John",
      lastName : "Doe",
      id     : 5566,
      fullName : function() {
        return this.firstName + " " + this.lastName;
      }
    };
    
    // 显示对象的数据
    document.getElementById("demo").innerHTML = person.fullName();
    </script>
    
    </body>
    </html>

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>菜鸟教程(runoob.com)</title>
    </head>
    <body>
    
    <h2>JavaScript <b>this</b> 关键字</h2>
    
    <p>实例中,<b>this</b> 指向了 <b>person</b> 对象。</p>
    <p>因为 person 对象是 fullName 方法的所有者。</p>
    
    <p id="demo"></p>
    
    <script>
    // 创建一个对象
    var person = {
      firstName: "John",
      lastName : "Doe",
      id     : 5566,
      fullName : function() {
        return this.firstName + " " + this.lastName;
      }
    };
    
    // 显示对象的数据
    document.getElementById("demo").innerHTML = person.fullName();
    </script>
    
    </body>
    </html>

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>菜鸟教程(runoob.com)</title>
    </head>
    <body>
    
    <h2>JavaScript <b>this</b> 关键字</h2>
    
    <p>实例中,<b>this</b> 指向了 window 对象:</p>
    
    <p id="demo"></p>
    
    <script>
    var x = this;
    document.getElementById("demo").innerHTML = x;
    </script>
    
    </body>
    </html>

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>菜鸟教程(runoob.com)</title>
    </head>
    <body>
    
    <h2>JavaScript <b>this</b> 关键字</h2>
    
    <p>实例中,<b>this</b> 指向了 window 对象:</p>
    
    <p id="demo"></p>
    
    <script>
    "use strict";
    var x = this;
    document.getElementById("demo").innerHTML = x;
    </script>
    
    </body>
    </html>

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>菜鸟教程(runoob.com)</title>
    </head>
    <body>
    
    <h2>JavaScript <b>this</b> 关键字</h2>
    
    <p>实例中,<b>this</b> 表示 myFunction 函数的所有者:</p>
    
    <p id="demo"></p>
    
    <script>
    document.getElementById("demo").innerHTML = myFunction();
    function myFunction() {
      return this;
    }
    </script>
    
    </body>
    </html>

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>菜鸟教程(runoob.com)</title>
    </head>
    <body>
    
    <h2>JavaScript <b>this</b> 关键字</h2>
    
    <button onclick="this.style.display='none'">点我后我就消失了</button>
    
    </body>
    </html>

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>菜鸟教程(runoob.com)</title>
    </head>
    <body>
    
    <h2>JavaScript <b>this</b> 关键字</h2>
    
    <p>在实例中,<b>this</b> 指向了 fullName 方法所属的对象 person。</p>
    
    <p id="demo"></p>
    
    <script>
    // 创建一个对象
    var person = {
      firstName  : "John",
      lastName   : "Doe",
      id     : 5566,
      myFunction : function() {
        return this;
      }
    };
    
    // 显示表单数据
    document.getElementById("demo").innerHTML = person.myFunction();
    </script>
    
    </body>
    </html>

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>菜鸟教程(runoob.com)</title>
    </head>
    <body>
    
    <h2>JavaScript <b>this</b> 关键字</h2>
    
    <p>实例中,<b>this</b> 指向了 <b>person</b> 对象。</p>
    <p>因为 person 对象是 fullName 方法的所有者。</p>
    
    <p id="demo"></p>
    
    <script>
    // 创建一个对象
    var person = {
      firstName: "John",
      lastName : "Doe",
      id     : 5566,
      fullName : function() {
        return this.firstName + " " + this.lastName;
      }
    };
    
    // 显示对象的数据
    document.getElementById("demo").innerHTML = person.fullName();
    </script>
    
    </body>
    </html>

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>菜鸟教程(runoob.com)</title>
    </head>
    <body>
    
    <h2>JavaScript this 关键字</h2>
    <p>实例中 <strong>this</strong> 指向了 person2,即便它是 person1 的方法:</p>
    
    <p id="demo"></p>
    
    <script>
    var person1 = {
      fullName: function() {
        return this.firstName + " " + this.lastName;
      }
    }
    var person2 = {
      firstName:"John",
      lastName: "Doe",
    }
    var x = person1.fullName.call(person2); 
    document.getElementById("demo").innerHTML = x; 
    </script>
    
    </body>
    </html>

  • 相关阅读:
    Eclipse检出原MyEclipse项目后 javax.servlet.http相关类都报错【我,体现着一类jar包问题的处理方法】
    Tomcat中加载不到项目 项目构建Deployment Assembly报错:The given project is not a virtual component project
    Unity手游之路<十一>资源打包Assetbundle
    Unity手游之路<十二>手游资源热更新策略探讨
    Unity手游之路<十三>手游代码更新策略探讨
    观察者(Observer)模式
    访问者(Visitor)模式
    设计模式(15)-Facade Pattern
    使用Monitor调试Unity3D Android程序日志输出(非DDMS和ADB)
    思维导图快捷键
  • 原文地址:https://www.cnblogs.com/tszr/p/10942981.html
Copyright © 2020-2023  润新知