Access to Map values in Safari 7.1+

前端 未结 1 430
南方客
南方客 2021-01-19 14:54

Safari has support for ES6 Maps and Sets in version 7.1 and higher (MDN). I\'m using a Map in my application and at some point want access to the values. Calling the v

相关标签:
1条回答
  • 2021-01-19 15:49

    This indeed seems to be a bug with Safari 7.1 and 8. I managed to work around this issue by first checking if the next function was available and if not, I used the for...of construct. Because this is invalid syntax pre-ES6, I had to wrap it in an eval statement:

    m = new Map();
    m.set(1, "test");
    m.set("2", false);
    it = m.values(); // returns an Iterator
    if (typeof it.next === 'function') {
        v = it.next();
        // do something with v here
    } else {
        eval("for (v of iterator) /* do something with v here */");
    }
    
    0 讨论(0)
提交回复
热议问题