c++qtqjsengine

QJSValue call Array.prototype.push not working


I want to evaluate my JS array through QJSValue and call required methods. For example: push, splice, etc... But calling Array.prototype.push (or others) has no effect.

//QQmlApplicationEngine myEngine;
auto arr = myEngine->evaluate("[1, 2, 3]");
qDebug() << arr.isArray(); //true
qDebug() << arr.property("length").toInt(); //3
qDebug() << arr.property("push").call({4, 5, 6}); //call Array.prototype.push
qDebug() << arr.property("length").toInt(); //3

What's wrong?


Solution

  • arr.property("push").call({4, 5, 6});
    

    looks like to be JS with this set to the Array instance:

    Array.protoype.push(4, 5, 6);
    

    Try to call the property with this set to the arr instance:

    arr.property("push").callWithInstance(arr, {4, 5, 6});