简体   繁体   中英

On Node.js v 8.9.3 the method Array.concat on extended class remove empty values - Why?

I am trying to add some new power to Arrays into node.js without changing the Array class avoiding some unexpected impacts into others modules.

When I start testing my code, I found some failures that I finally could find the reason. The concat method behaves differently after extended.

This is completely strange to me. This is an expected behavior?

Very short version to see the problem:

 class MyArray extends Array{}; /* * Test with values */ var sizeValueArray = new Array(4,4,4).concat([1,2,3]).length; var sizeValueMyArray = new MyArray(4,4,4).concat([1,2,3]).length; console.log( "size from concat values array = ", sizeValueArray ); console.log( "size from concat values my array = ", sizeValueMyArray ); if( sizeValueArray != sizeValueMyArray ) { console.log("this is strange"); } else { console.log("as expected.") } /* * Test with empty values */ var sizeEmptyArray = new Array(5).concat(new Array(6) ).length; var sizeEmptyMyArray = new MyArray(5).concat(new MyArray(6) ).length; console.log( "size from concat empty array = ", sizeEmptyArray); console.log( "size from concat empty my array = ", sizeEmptyMyArray ); if( sizeEmptyMyArray != sizeEmptyArray ) { console.log("this is strange"); } else { console.log("as expected.") } 

You can see that in the browser, the result of this two operations is the same. But not in the node.js, as you can see here https://repl.it/@thiagodamata/ArrayConcatStrangeBehaviorOnExtend

在此处输入图片说明

Looks like this bug was fixed in the recent versions. You can see that working in this repl . So, it was a bug that is already fixed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM