How to create a 'real' JavaScript array in Rhino

前端 未结 2 967
庸人自扰
庸人自扰 2021-01-05 12:23

Okay, I\'m a little stumped. I\'m probably missing something blatantly obvious but apparently I just can\'t see the forest for the trees:

I\'m trying to call a JavaS

2条回答
  •  萌比男神i
    2021-01-05 13:08

    Almost forgot: Object.prototype.toString.call(a) returns [object Array]

    Okay, that's the crucial information. That tells us that the array really is an array, it's just that it's being initialized by an Array constructor in a different scope than the one that the function is testing for, exactly as though you were testing an array from one window against another window's Array constructor in a browser-based app. E.g., there's a scope problem.

    Try replacing

    Object[] fArgs = new Object[]{ new NativeArray(0) };
    

    with

    Object[] fArgs = new Object[]{ cx.newArray(scope, 0) };
    

    ...to ensure the correct Array constructor is used. Because you've gone directly to the NativeArray constructor, you've bypassed ensuring that its scope is right, and so the array object's constructor is an Array constructor, but not the same Array constructor as the one on the global object the function sees.

提交回复
热议问题