zoukankan      html  css  js  c++  java
  • 前端this相关

    前端this相关:

    <script>
        //示例一
        function func1() {
            console.log(this);  //this代指window
        }
        func1();
        //window.func1();
    
    
        //示例二
        function Func() {
            this.name = "jia";  //this代指obj
        }
        var obj = new Func();
        console.log(obj.name);  //jia
    
    
        //示例三
        function Func2() {
            this.name = "kaylee";
            this.show = function () {
                console.log(this);  //this代指obj2
            }
        }
        var obj2 = new Func2();
        obj2.show();
    
    
        //示例四
        var userInfo = {
            name: "jia",
            age: 18,
            show: function () {
                console.log(this);  //this代指userInfo
            }
        };
        userInfo.show();
    
    
        //示例五
        var userInfo2 = {
            name: "kaylee",
            age: 18,
            show: function () {
                console.log(this);  //this代指userInfo2
                (function () {
                    console.log(this);  //this代指window
                })()
            }
        };
        userInfo2.show();
    
    
        //示例五可以写作:
        var userInfo3 = {
            name: "kaylee",
            age: 18,
            show: function () {
                console.log(this);  //this代指userInfo3
                inner();
            }
        };
        userInfo3.show();
    
        function inner() {
            console.log(this);  //this代指window
        }
    
    
        //示例六
        var userInfo4={
            name:"kaylee",
            age:18,
            show:function () {
                console.log(this);  //this代指userInfo4
                var that = this;
                (function () {
                    console.log(that);  //that代指userInfo4
                })()
            }
        };
        userInfo4.show();
    </script>

    总结:

      函数被 对象.函数 执行,那么函数中的this就是该对象

  • 相关阅读:
    TP6|TP5.1 PHPoffice导出|导入
    centOS 7 环境搭建之安装 Redis
    centOS 7 环境搭建之安装 MySQL
    双向循环链表(DoubleLoopLinkList)
    双向链表(DoubleLinkList)
    可执行程序的编译过程
    C语言文件操作
    C语言跨平台时间操作计算时间差
    C语言线程安全问题
    C++类型双关
  • 原文地址:https://www.cnblogs.com/metianzing/p/7865902.html
Copyright © 2011-2022 走看看