为什么let this=that

版权声明:我写的你不能转载,但是你可以复制啊。复制记得加关注啊(迷之微笑)。 https://blog.csdn.net/quhongqiang/article/details/82979984
不知道楼主有没有接触过jquery jquery里边有一个特别典型的例子能说明用_this的作用
$("#btn").click(function(){
    var that = this;//这里this和that都代表了"#btn"这个对象
    $(".tr").each(function(){
          this;//在这里this代表的是每个遍历到的".tr"对象
          that;//仍代表"#btn"对象
    })
})
这种情况就是在一个代码片段里this有可能代表不同的对象,而编码者希望_this代表最初的对象
let that = this;
    var ajax = new XMLHttpRequest();
    ajax.open('get','http://127.0.0.1:24010/ZKIDROnline/ScanReadIdCardInfo?OP-DEV=1&CMD-URL=4');
    ajax.send();
    ajax.onreadystatechange = function () {
       if (ajax.readyState===4 &&ajax.status===200) {
         /*eslint-disable no-eval */   //这个注释必须要
          var data = eval("(" + ajax.responseText + ")");
          let res = data.Certificate;
          if (res) {
            that.setState({
              value: res
            })
          }
      }
    }

猜你喜欢

转载自blog.csdn.net/quhongqiang/article/details/82979984
let