繁体   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?

我试图在不更改Array类的情况下为Arrays添加一些新功能到node.js中,以避免对其他模块产生意外影响。

当我开始测试代码时,我发现了一些失败,最终我找到了原因。 扩展后, concat方法的行为有所不同。

这对我来说是完全陌生的。 这是预期的行为吗?

很短的版本看问题:

 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.") } 

您可以在浏览器中看到,这两个操作的结果是相同的。 但不在node.js中,如您在此处看到的https://repl.it/@thiagodamata/ArrayConcatStrangeBehaviorOnExtend

在此处输入图片说明

看来此错误已在最新版本中修复。 您可以看到此repl中的工作。 因此,这是一个已修复的错误。

暂无
暂无

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

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