Comments for JavaScript UXO Removal Updated
Yes, you'd get this behavior:
printMessage(); //”Whatup, yo?” o.printMessage(); //”Hello world!”
you implicitly get the "self.printMessage" since there's no weak ThisBinding for the call context so "this" points at window (or whatever your global is called).
I think this is one of the more lucid proposals for addressing the mysterious "this" problem in JavaScript. I'd even go so far as to say I like it. In most of the code I've written, these changes would have no side effect. I am curious, though, how this change would affect functions that begin in a non-pathological state, such as:
var msg = "Whatup, yo?";
function printMessage(){ console.log(this.msg); }
var o = { msg: "Hello world!" };
printMessage(); //"Whatup, yo?" or "Hello world!"?
o.printMessage = printMessage();
o.printMessage(); //"Hello world!"?
Should printMessage() be treated the same as self.printMessage()?
Since you'll be getting
foo
back via the MyClass
instance, it gets a new weak binding thanks to the dot operator innocuously planted in there, meaning the ThisBinding
is the MyClass
instance.
Things still do what you expect.
MyClass.prototype.foo = YourClass.prototype.foo;
Should calling new MyClass().foo keep the weak ref to YourClass.prototype?