Skip to content

Checking whether an array's constructor is === Array fails #7892

Closed
@ZeroXLR

Description

@ZeroXLR
  • Version: v6.3.1
  • Platform: Darwin xxxxx.local 15.6.0 Darwin Kernel Version 15.6.0: Thu Jun 23 18:25:34 PDT 2016; root:xnu-3248.60.10~1/RELEASE_X86_64 x86_64

Problem Statement

The simplest instance of the problem is this: I have a file, say test.js in my Desktop, containing the following code:

module.exports = function(a) {
    return a.constructor === Array;
};

When I run this in the node REPL, it gives me an odd result:

xxxxx@xxxxx:~/Desktop$ node -v
v6.3.1
xxxxx@xxxxx:~/Desktop$ node
> var t = require('./test.js');
undefined
> t([1, 2]);  <<<--- Passing in [1, 2] whose constructor should be Array
false          <<<--- What?!
> [1, 2].constructor === Array;
true
> var tprime = function(a) {return a.constructor === Array;};
undefined
> tprime([1, 2]);
true

Since t is simply checking whether the constructor of its argument is Array, I was expecting a true when you passed in [1, 2] whose constructor is, in fact, an Array.

Running Problem Code against LTS

I ran the exact same test after uninstalling v6.3.1 and installing v4.4.7 and the problem disappears:

xxxxx@xxxxx:~/Desktop$ node -v
v4.4.7
xxxxx@xxxxx:~/Desktop$ node
> var t = require('./test.js');
undefined
> t([1, 2]);
true          <<<--- As expected
> [1, 2].constructor === Array;
true
> var tprime = function(a) {return a.constructor === Array;};
undefined
> tprime([1, 2]);
true
> 

Checking against String

The problem also disappears if I change test.js to check against String instead of Array (in both v6.3.1 and v4.4.7). Here is the new test.js:

module.exports = function(a) {
    return a.constructor === String;
};

And here are the checks in REPL:

xxxxx@xxxxx:~/Desktop$ node -v
v6.3.1
xxxxx@xxxxx:~/Desktop$ node
> var t = require('./test.js');
undefined
> t("sdfdsf");
true          <<<--- No issue this time
> "sdfdsf".constructor === String;
true
> .exit
xxxxx@xxxxx:~/Desktop$ node -v
v4.4.7
xxxxx@xxxxx:~/Desktop$ node
> var t = require('./test.js');
undefined
> t("sdfdsf");
true
> "sdfdsf".constructor === String;
true
> .exit
Veritas@nfr:~/Desktop$

Conclusion

Thus, in conclusion, is there a real bug here or is this a known valid difference between Node versions v4.x.x and #v6.x.x?

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateIssues and PRs that are duplicates of other issues or PRs.replIssues and PRs related to the REPL subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions