V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
linxiaoziruo
V2EX  ›  问与答

es 中关于 this 指针丢失的问题

  •  
  •   linxiaoziruo · 2018-11-06 14:41:40 +08:00 · 1296 次点击
    这是一个创建于 2004 天前的主题,其中的信息可能已经有所发展或是发生改变。

    let a = { name: ‘ zaki ’, say: function(param) { console.log(param); console.log(this); console.log(this.name); return param; } };

    (function() { a.say(); //可以打印出符合期望的的 this 信息 let fn = a.say; fn(1); // 不能打印出符合期望的 this 信息 })()

    4 条回复    2018-11-07 17:09:05 +08:00
    ookkxw
        1
    ookkxw  
       2018-11-06 14:51:12 +08:00 via iPhone
    看你不知道的 js 基础问题,
    Tapir
        2
    Tapir  
       2018-11-06 18:01:30 +08:00   ❤️ 1
    基础知识
    call apply bind 都能解决问题
    ES 顺序看
    12.3.4Function Calls
    12.3.4.2Runtime Semantics: EvaluateCall ( func, ref, arguments, tailPosition )
    7.3.12Call ( F, V [ , argumentsList ] )
    9.2.1[[Call]] ( thisArgument, argumentsList )
    9.2.1.2OrdinaryCallBindThis ( F, calleeContext, thisArgument )
    这几部分
    关键点
    If thisArgument is undefined or null, then
    Let globalEnv be calleeRealm.[[GlobalEnv]].
    Let globalEnvRec be globalEnv's EnvironmentRecord.
    Assert: globalEnvRec is a global Environment Record.
    Let thisValue be globalEnvRec.[[GlobalThisValue]].
    hanzichi
        3
    hanzichi  
       2018-11-06 19:07:14 +08:00   ❤️ 1
    linxiaoziruo
        4
    linxiaoziruo  
    OP
       2018-11-07 17:09:05 +08:00
    老哥们,弄清楚了。多谢各位
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2269 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 07:12 · PVG 15:12 · LAX 00:12 · JFK 03:12
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.