Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ZeroXLR opened this issue Jul 27, 2016 · 2 comments
Closed

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

ZeroXLR opened this issue Jul 27, 2016 · 2 comments
Labels
duplicate Issues and PRs that are duplicates of other issues or PRs. repl Issues and PRs related to the REPL subsystem.

Comments

@ZeroXLR
Copy link

ZeroXLR commented Jul 27, 2016

  • 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?

@addaleax addaleax added duplicate Issues and PRs that are duplicates of other issues or PRs. repl Issues and PRs related to the REPL subsystem. labels Jul 27, 2016
@addaleax
Copy link
Member

See #7788, this will be fixed in the next v6.x release. I’m closing this as a duplicate, but feel free to ask follow-up questions!

@ZeroXLR
Copy link
Author

ZeroXLR commented Jul 27, 2016

Glad to get a quick reply. I have no further questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate Issues and PRs that are duplicates of other issues or PRs. repl Issues and PRs related to the REPL subsystem.
Projects
None yet
Development

No branches or pull requests

2 participants