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