ES6 中 Proxy 的 getPrototypeOf 陷阱是怎么回事? - V2EX
Sparetire

ES6 中 Proxy 的 getPrototypeOf 陷阱怎么回事?

  •  1
     
  •   Sparetire Mar 23, 2019 4751 views
    This topic created in 2625 days ago, the information mentioned may be changed or developed.

    如下代码

    function Test() { } const a = new Proxy({}, { getPrototypeOf() { return Array.prototype; } }); console.log(a instanceof Array); // true const b = new Proxy({}, { getPrototypeOf() { return Test.prototype; } }); console.log(b instanceof Test); // false console.log(b instanceof Test); // true console.log(b instanceof Test); // true 

    为什么 a, b 两个的结果完全不一样, 原生类和我自己定义的类难道有什么区别?

    而对于后面两个结果, 难道 getPrototypeOf 存在副作用?

    另一个例子如下

    class Test {} const a = new Proxy({}, { getPrototypeOf() { return Array.prototype; } }); console.log(a instanceof Array); // true const b = new Proxy({}, { getPrototypeOf() { return Test.prototype; } }); console.log(b instanceof Test); // true console.log(b instanceof Test); // true console.log(b instanceof Test); // true 

    如果把函数换成 class, 则出现了预期之中的结果, 但是 js 中 class 不是仅仅是语法糖吗? 为什么会有这样的区别?

    Supplement 1    Mar 23, 2019
    补充环境
    Node v10.15.1 64bit 和 Chrome 73.0.3683.86 复现
    FF 66.0.1 64bit 则全都是 true, 符合预期
    Supplement 2    Mar 23, 2019

    再补充下, 使用 Object.getPrototypeOf() 则结果都符合预期, 出问题的只有 instanceof

    function Test() { } const a = new Proxy({}, { getPrototypeOf() { return Array.prototype; } }); console.log(Object.getPrototypeOf(a) === Array.prototype); // true const b = new Proxy({}, { getPrototypeOf() { return Test.prototype; } }); console.log(Object.getPrototypeOf(b) === Test.prototype); // true console.log(Object.getPrototypeOf(b) === Test.prototype); // true console.log(Object.getPrototypeOf(b) === Test.prototype); // true 
    12 replies    2019-03-24 23:34:06 +08:00
    jyz19880823
        1
    jyz19880823  
       Mar 23, 2019
    这应该是 chrome 和 v8 的 bug 吧
    至少 safari 和 firefox 都是好的
    Sparetire
        2
    Sparetire  
    OP
       Mar 23, 2019
    @jyz19880823 看起来的确是的...FF 下面的结果都是 true, 符合期望
    azh7138m
        3
    azh7138m  
       Mar 23, 2019
    v11.12 同复现,很有趣,也觉得是 v8 的 bug
    Sparetire
        4
    Sparetire  
    OP
       Mar 23, 2019
    @azh7138m 而且只有 instanceof 有这么个问题, Object.getPrototypeOf()就很正常...
    ayase252
        5
    ayase252  
       Mar 23, 2019 via iPhone
    有趣的一点是如果在第一个例子 console.log(b instance of Test)之前加一行 Object.getPrototypeOf(b)之后又很正常。我倾向认为是引擎的 bug ?
    azh7138m
        6
    azh7138m  
       Mar 23, 2019   1
    这里的 b instanceof Test 等效于 Test[Symbol.hasInstance].call(Test, b)
    Object.getPrototypeOf 则是取的[[Prototype]]
    两者不同
    具体的我不知道今天能不能看完
    Sparetire
        7
    Sparetire  
    OP
       Mar 23, 2019
    @ayase252 是的, 并且在下面这个例子中也会有区别
    ```Javascript
    function Test() {
    return new Proxy({}, {
    getPrototypeOf() {
    return Test.prototype;
    }
    });
    }

    const a = Test();
    console.log(a instanceof Test); // false

    const b = new Test();
    console.log(b instanceof Test); // true
    ```
    然而同样 FF 中则都是 true, 所以也倾向于是引擎的 bug 吧
    不过真是走狗屎运, 在生产上碰到这么个 bug...
    azh7138m
        8
    azh7138m  
       Mar 24, 2019   1
    azh7138m
        9
    azh7138m  
       Mar 24, 2019
    顺便更正下我 6L 的结论

    https://www.ecma-international.org/ecma-262/9.0/#sec-object.getprototypeof

    所以 Object.getPrototypeOf(b) 并不是取的[[Prototype]],而是 b.[[GetPrototypeOf]](),所以结果都符合预期
    Sparetire
        10
    Sparetire  
    OP
       Mar 24, 2019   1
    @azh7138m
    @ayase252
    @jyz19880823
    hax 大佬说这里是 V8 的一个 bug
    https://www.zhihu.com/question/317137493?rf=317137501
    So, 结帖
    MinonHeart
        11
    MinonHeart  
       Mar 24, 2019 via iPhone
    v8 优化带来的额外 bug,这也不是一个两个了,之前有个 canvas 的 bug 至今没修。直接去 crbug 提交 bug
    Sparetire
        12
    Sparetire  
    OP
       Mar 24, 2019 via Android
    @MinonHeart 嗯,提交了
    About     Help     Advertise     Blog     API     FAQ     Solana     2955 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 51ms UTC 03:14 PVG 11:14 LAX 20:14 JFK 23:14
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86