3 ms·
For one, "this" could refer to anything at any time, depending on who invoked the enclosing function. function myFunc() { console.log(this); }
by jestar_jokin 11y ago
For one, "this" could refer to anything at any time, depending on who invoked the enclosing function.
function myFunc() {
console.log(this);
}
var myObj = {};
// this = window
myFunc();
// this = window
myFunc.apply(null, []);
// this = myObj
myFunc.apply(myObj, []);
myObj.myFunc = myFunc;
// this = myObj
myObj.myFunc();
var tmpFunc = myObj.myFunc;
// this = window
tmpFunc();
Another issues is closures, where variables and properties available to nested functions are dependent on the outer functions. Combined with the floating "this", leads to highly dynamic behaviour which can only be assessed at runtime.
- userbinator 11y agoI'd guess the names of properties being possibly dynamically computed also makes things difficult to optimise.
- jestar_jokin 11y agoA clever compiler might be able to infer "structural typing", which is basically "duck typing" done at compile time. So, if you add a property "bar" to an object "foo", and you have some other code that calls "baz.bar", you can infer that "foo" is of type "HasABar", and "baz" is of type "HasABar", therefore it might be safe to use "foo" in place of "baz". The tricky part would be consolidating every structural subtype into cohesive classes.