Khan / live-editor

A browser-based live coding environment.
Other
763 stars 183 forks source link

`new` is not detecting explicitly bound functions #708

Open smj-edison opened 5 years ago

smj-edison commented 5 years ago

I was running examples from You Don't Know JS, and when I plugged this into the live editor, it gave the wrong results:

var foo = function(something) {
    this.a = something;
};

var obj1 = {};

var bar = foo.bind( obj1 );
bar( 2 );
println( obj1.a ); // expected 2, actual 2

var baz = new bar( 3 );
println( obj1.a ); // expected 2, actual 3
println( baz.a ); // expected 3, actual undefined

I confirmed that it normally works by replacing println with console.log, and running it in the debugger. The debugger gave all the expected values. Something with foo.bind isn't working correctly (which doesn't make sense, because it returns native code), or the new constructor isn't accounting for function bindings.

EDIT: added example: https://www.khanacademy.org/computer-programming/_/6379005266198528

EricBalingit commented 5 years ago

The editor transforms your code and replaces new in the ast with a custom instantiation mechanism. This is why you're seeing the results you're getting. Function.bind is working correctly, it's this instantiation mechanism that has been plagued with issues like this for quite a while.