推荐关注
› Meteor
› JSLint - a JavaScript code quality tool
› jsFiddle
› D3.js
› WebStorm
推荐书目
› JavaScript 权威指南第 5 版
› Closure: The Definitive Guide
pyplayer

Js 小白求问大佬们为何第八行 await 运行后 e 这个事件对象会变 null

  •  
  •   pyplayer · Apr 7, 2020 · 3408 views
    This topic created in 2407 days ago, the information mentioned may be changed or developed.
    const clickZan = async function (e) {
        if (!token) {
          alert("请先登录");
        } else {
          console.log(e.target);
          const commentid = e.target.getAttribute("commentid");
          debugger;
          const result = await reqZan(token, commentid);
          e.target.className = "iconfont icon-zan red";
        }
      };
    
    6 replies  •  2020-04-07 10:15:00 +08:00
    yyss8
    yyss8
    Apr 7, 2020 via iPhone
    1
    断点里也是 null ? 如果只是 console 出来的是 null 那么可以深度复制一个 e 的内容 然后 console 那个 e
    des
    des
    Apr 7, 2020 via Android
    2
    是 e.target 变成了 null 吧?
    e 变成 null 我觉得不太可能,建议把 e.target 首先赋值成一个变量
    pyplayer
    pyplayer
    OP
    Apr 7, 2020
    3
    @des
    @yyss8
    e 确实没变 null e.target 变 null 了 后来先把 e.target 赋给 temp 是可以的,不过警告了
    arning: This synthetic event is reused for performance reasons. If you're seeing this, you're accessing the property `target` on a released/nullified synthetic event. This is set to null. If you must keep the original synthetic event around, use event.persist().
    查了下 如果在 react 中想异步访问事件属性(如在 setTimeout 内),应该在是处理事件时调用 event.persist(),这会从事件池中移除该合成函数并允许对该合成事件的引用被保留下来
    des
    des
    Apr 7, 2020
    4
    @pyplayer
    料到了是用了框架
    既然用了 React,那就走 React 流程,不建议自己操纵 dom
    rodjl
    rodjl
    Apr 7, 2020 via iPhone
    5
    用了 react 吧?调用 e.persist()或者在 await 前用个变量保存 e.target
    zhw2590582
    zhw2590582
    Apr 7, 2020❤️1
    6
    react 内部是封装过事件对象的,事件对象是共用的,异步调用的情况下对象可能被释放或者重置,可以在异步之前缓存下 target 就可以了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2331 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 00:50 · PVG 08:50 · LAX 16:50 · JFK 19:50
    ♥ Do have faith in what you're doing.