Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

instanceof Array of array from vm script not working #37921

Closed
joshxyzhimself opened this issue Mar 26, 2021 · 3 comments
Closed

instanceof Array of array from vm script not working #37921

joshxyzhimself opened this issue Mar 26, 2021 · 3 comments

Comments

@joshxyzhimself
Copy link

Hi, is this the expected behaviour?

Was expecting test_data.test_array instanceof Array to return true

const vm = require('vm');
const test_data = new vm.Script(`
  test_data = { test_array: [] };
`).runInNewContext();

console.log(JSON.stringify({ test_data }, null, 2));
// {
//   "test_data": {
//     "test_array": []
//   }
// }

console.log(typeof test_data.test_array);
// object

console.log(test_data.test_array instanceof Array);
// false, should be true
@Trott
Copy link
Member

Trott commented Mar 26, 2021

runInNewContext() means that the Array in the new context is different from the Array in the calling context so this behavior is expected. It's similar to the way instanceof doesn't work as you might expect when using data in iframes in the browser.

@Trott
Copy link
Member

Trott commented Mar 26, 2021

I'm going to close but there's more info at nodejs/node-v0.x-archive#1277 including a note that Array.isArray() is probably what you want to use.

@Trott Trott closed this as completed Mar 26, 2021
@joshxyzhimself
Copy link
Author

That makes sense lol, should've googled more. Thanks a lot @Trott!

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

No branches or pull requests

2 participants