简体   繁体   English

在Node.js v 8.9.3上,扩展类上的Array.concat方法删除空值-为什么?

[英]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. 我试图在不更改Array类的情况下为Arrays添加一些新功能到node.js中,以避免对其他模块产生意外影响。

When I start testing my code, I found some failures that I finally could find the reason. 当我开始测试代码时,我发现了一些失败,最终我找到了原因。 The concat method behaves differently after extended. 扩展后, concat方法的行为有所不同。

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 但不在node.js中,如您在此处看到的https://repl.it/@thiagodamata/ArrayConcatStrangeBehaviorOnExtend

在此处输入图片说明

Looks like this bug was fixed in the recent versions. 看来此错误已在最新版本中修复。 You can see that working in this repl . 您可以看到此repl中的工作。 So, it was a bug that is already fixed. 因此,这是一个已修复的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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