Skip to content
This repository has been archived by the owner on Sep 30, 2019. It is now read-only.

JavaArray incorrectly extends native Array constructor - causes native functions to not work #76

Open
marklawlor opened this issue Jan 31, 2019 · 0 comments

Comments

@marklawlor
Copy link
Contributor

marklawlor commented Jan 31, 2019

I found this issue a couple of days ago but thought it must be a non-issue, but since #70 it has appeared more often.

The JavaArray class extends Array but uses a different constructor signature. Because of this difference, native Array functions cannot work on JavaArrays (especially functions that return new instances of an array such as map).

Internally the native Array.Map function may initialise an Array with new Array(arrayLength) and then populate the indexes. As JavaArray assumes the first parameter will be an array and not a number, it fails on the forEach call.

For example

class NewArray extends Array { constructor(...elements) { super(...elements); console.log('i was called with:', ...elements); } }
const test1 = new NewArray()
// logs: I was called with:
test1.map(() => 1)
// logs: I was called with: 0
const test2 = new NewArray("a", "b", "c")
// logs: I was called with: "a", "b", "c"
test2.map(() => 1)
// logs: I was called with: 3

An alternative way to resolve this issue would be to re-implement the native functions in the JavaArray class

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant