• js中的this


    我们在js中研究的this,主要是研究函数中的this

    1、自执行函数中的this永远是window
     var obj = {
    fn: (function () {
    console.log(this);//this-->window
    return 12;
    })()
    };
    2、给元素的事件绑定方法,当触发事件执行对应方法的时候,方法中的this是当前的元素

     oDiv.onclick=function(){
    //this-->oDiv
    }


    3、不管函数是在哪里面执行的,我们就看函数名之前是否有".",有的话,"."前面是谁this就是谁,没有的话this就是window



        oDiv.onclick = (function () {
    console.log(this);//this-->window
    return function () {
    console.log(this);//this-->oDiv
    }
    })();

    function fn() {
    console.log(this);//this-->window
    }
    oDiv.onclick = function () {
    //this-->oDiv
    fn();
    }
     4.构造函数中this指的是当前实例本身。
    (未完待续)
     
  • 相关阅读:
    functools.partial偏函数
    python之路——模块和包
    异常
    递归函数
    内置函数和匿名函数
    列表推导式和生成器表达式
    迭代器和生成器
    Parentheses Balance
    poj1363 Rails
    hud1237 简单计算器
  • 原文地址:https://www.cnblogs.com/jingjing0518/p/4756556.html
Copyright © 2020-2023  润新知