var player = {
play: function(){
var self = this;
setTimeout(function(){
console.log(self);
});
}
} var player = {
play: function(){
var self = this;
setTimeout(function(){
console.log(self);
});
}
}On a sidenote, I'm trying to get away from using self, and do object.bind in all my setTimeouts. To my mind, it leads to much cleaner code.
function wait(o){
return setTimeout(function(){ o.action.call(o.this) },o.delay*1000)
}
To use it like this: wait({
action : function(){ console.log(this) },
delay : 1,
this : this
});Since .bind is essentially currying, you could imaginably do window.setTimeout(this.foo.bind(this, bar), baz) as well, if foo is dependent on bar (timing out a request `bar' or something like that).
var play = player.play;
play();
"self" will still strangely bind to "window" if not in strict mode. player[playing ? "play" : "pause"]()
(a not-so-weird JS pattern) would be the same as: (playing ? player.play : player.pause)()I think most people would write it like this:
playing ? player.play() : player.pause()Not sure if i understand the question. This seems to work in Python:
s = "Hello"
up = True
(s.upper if up else s.lower)()
Not that it's a very pythonic piece of code, but it works, and i would expect many other languages support something similar.Anyway, that's tangential. And i wasn't discussion what "most people would" do either. What i was trying to say is that by aliasing "var self = this" you're not automatically immune to the quirks of "this" in JS.
o.f()
Can be decomposed in Python: func = o.f
func()
JavaScript fails this test, and it's a shame. NodeList.prototype.map = Array.prototype.map
Just one line and now you can do stuff like: document.querySelectorAll("div").map(function(ele){
return ele.id
});I don't know if it's really that bad that JS returns the unbound function when doing `obj.someFunc`, as it is consistent with any other property access: it returns the same thing that was assigned to that key in the object, or at some point in its prototype chain.
The really broken behavior, at least for me, is that when doing `var f = obj.someFunc(); f()`, the "this" gets bound to "window" in the f() call, instead of being null, or better yet raising an error whenever referenced, like an undefined variable.