0

Using Rhino 1.7.6, this script works:

input = [ { foo: [ "aaa", "bbb", "ccc" ] } ];
var output = [];
input.forEach(function (obj) {
    obj.foo.forEach(function(s) { output.push(s); });
});

but this one does not:

input = [ { foo: [ "aaa", "bbb", "ccc" ] } ];
var output = [];
input.forEach(function (obj) {
    obj.foo.forEach(output.push);
});

instead, throwing this exception on line 4:

org.mozilla.javascript.EvaluatorException: Cannot modify a property of a sealed object: 0.

Why?

I feel like I'm missing something obvious here...

Archie
  • 4,959
  • 1
  • 30
  • 36
  • 1
    It isn't related to Rhino, it is how JS behaves: `this` lost its context (try in a browser). Does this answer your question? [JavaScript Callback Scope](https://stackoverflow.com/questions/183214/javascript-callback-scope) – sp00m Apr 27 '21 at 16:41
  • 1
    So in other words, the plain expression `output.push` returns a function that has "forgotten" that it was ever associated with `output`... That would explain it. Thanks! – Archie Apr 27 '21 at 17:22

0 Answers0