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

Reflect.construct permanently corrupts the invoked constructor #3217

Closed
shicks opened this issue Jun 23, 2017 · 0 comments
Closed

Reflect.construct permanently corrupts the invoked constructor #3217

shicks opened this issue Jun 23, 2017 · 0 comments
Assignees
Labels
Milestone

Comments

@shicks
Copy link

shicks commented Jun 23, 2017

Repro:

<pre id="log"></pre>
<script>
function log(msg) { document.getElementById('log').textContent += msg + '\n'; }

function Base() {}
function Derived() {}
Derived.prototype = Object.create(Base.prototype);  // OPTIONAL

log(new Base() instanceof Derived);  // correctly logs 'false'
Reflect.construct(Base, [], Derived);
log(new Base() instanceof Derived);  // from now on, logs 'true'!
</script>

This bug is pretty elusive - if you ever open the developer tools (even just for a second) everything goes back to normal, and the Base constructor again constructs Base objects (hence the HTML logging).

It's also irrelevant whether Derived is a prototypal subclass of Base. Fun fact: if the Object.create call is left out, then in the broken state, new Base() instanceof Base actually returns false!

This seems to be a one-way switch: calling Reflect.construct(Base, [], Other) afterwards does not switch new Base() from creating Deriveds to creating Others. But it also doesn't work to try to workaround the issue with a proactive Reflect.construct(Base, [], Base), since a future call to Reflect.construct(Base, [], Derived) will still cause problems.

EDIT: Also, this bug only triggers if the base class has been instantiated at least once before calling Reflect.construct: so if the first log statement is removed then the second one logs 'false'.

@curtisman curtisman added this to the 1.6 milestone Jun 26, 2017
@curtisman curtisman added the Bug label Jun 26, 2017
brad4d pushed a commit to google/closure-compiler that referenced this issue Jun 29, 2017
Yannic pushed a commit to Yannic/com_google_closure_compiler that referenced this issue Jul 6, 2017
Yannic pushed a commit to Yannic/com_google_closure_compiler that referenced this issue Jul 6, 2017
@suwc suwc closed this as completed Jul 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants