Important: Please read the Qt Code of Conduct -

Help with JavaScript type

  • I'm asking this here because it may be peculiar to the QML JavaScript engine.

    What value of x could give me the following results:

    x !== null
    x !== undefined
    typeof x === "object"
    String(x) === "?"
    Object.getPrototypeOf(x) === undefined

    In my program x could be null, but the first test says otherwise, or it could be a Uint8Array, but the string version of that is a comma-separated list of numbers. What, other than a quoted question mark, displays as a question mark?

    I'm wondering if it could be a reference to an object that has been garbage collected, and I've discovered some sort of bug in the engine.

  • Lifetime Qt Champion


    Can you post a minimal code sample that shows the situation you describe ?

  • @pderocco
    Don't know, but don't forget you can try various instanceOf() if you can guess what the (base) type might be?

  • @SGaist Unfortunately, when I try a simple program that does what the real program is (supposedly) doing, it works fine. So I'm trying to deduce it from the other end, by figuring out what the type of the object is.

  • @JonB The only two things I should get are null or a Uint8Array. When the failure occurs, it doesn't compare equal to null, and x instanceof Uint8Array returns false. Also, x.prototype returns undefined. Other than null, what kinds of things can report a type of object, but not have a prototype?

    Update: Apparently, things like Uint8Array report a type of object, and have a prototype of undefined rather than null. I guessed that I may have used an ArrayBuffer instead of a Uint8Array, and sure enough, x instanceof ArrayBuffer returns true. So console.log displays an ArrayBuffer as a question mark.

    So, problem solved I guess. Thanks for pointing me to instanceof.

  • @pderocco
    Yep, that's why I suggested instanceOf() :) JS is odd!

Log in to reply