instanceof operator fails when passing an object through windows

后端 未结 1 1921
礼貌的吻别
礼貌的吻别 2021-01-02 23:22

In order to pass data between windows, I open new windows via the window.open method and set a property of the newly opened window to an object. This allows me

相关标签:
1条回答
  • 2021-01-03 00:00

    Since your window's Object and the source window's Object aren't the same thing, an instance of one won't be an instance of the other. You can use Object.prototype.toString to distinguish between objects and arrays:

    if(Object.prototype.toString.call(m) === '[object Array]') {
        // It's an array
    } else {
        // It's not
    }
    

    You can also use Array.isArray, if available.

    Here's a demo. (It uses an <iframe> instead of a popup, by the way.)

    0 讨论(0)
提交回复
热议问题