lyfx
V2EX  ›  2018

关于 ecmascript6 中的 proxy 的一个问题

  •  
  •   lyfx · Apr 22, 2018 · 2387 views
    This topic created in 2951 days ago, the information mentioned may be changed or developed.
    <script> "use strict"; let proxy = new Proxy( function() {}, { apply: function( a, b, c ) { return b; } } ); console.log( proxy() ); //undefined </script>

    问题如上,proxy 中 apply 方法第二个参数有啥用,为啥我调用的结果是 undefined? 官方的解释是:被调用时的上下文对象。本来不想拿上来问的,但是百度了很久,很多文章对这个都没实例,直接跳过,包括 MDN。

    1 replies    2018-04-22 19:02:08 +08:00
    zbinlin
        1
    zbinlin  
       Apr 22, 2018
    就是那个 this:
    const obj = { proxy: proxy };
    console.log(obj.proxy() === obj);
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5372 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 01:28 · PVG 09:28 · LAX 18:28 · JFK 21:28
    ♥ Do have faith in what you're doing.