3 ms·
> No it isn't. Its a function argument that is implicitly passed "left of the dot". That is true in JavaScript, but not in other languages that also have "this
by munificent 9y ago
> No it isn't. Its a function argument that is implicitly passed "left of the dot".
That is true in JavaScript, but not in other languages that also have "this" and closures. So the confusion comes from two issues: 1. JavaScript is inconsistent with other languages, and 2. It's not at all clear that JavaScript's semantics are actually good.
In other languages where "this" is not loosely bound, there is next-to-no confusion around them and no problems, so the evidence is there that the semantics JavaScript chose aren't user-friendly.
The reason JS has those semantics is because it (at the time) had no way to distinguish between a method declaration (where this should be dynamically bound) and a function declaration (where this should be lexically bound). Other languages with class syntax don't have that problem.
JavaScript does have class syntax now, but changing the semantics of how this works would have been very difficult this many years later.
- spion 9y agoAll languages have different semantics. Otherwise they would've been one language. Python has an explicit self that is unbound. Other languages have it implicit and bound... Etc
- twic 9y agoWhat do you mean by "unbound"? Some Python: >>> class Foo: >>> def __init__(self, x): self.x = x ... ... def bar(self): print('x =', self.x) ... ... >>> f = Foo(23) >>> b = f.bar >>> b() x = 23 Note that when you extract the method from the object as a callable, it hangs onto the right self. Same deal as Java: class Foo { private int x; Foo(int x) { this.x = x; } void bar() { System.out.println("x = " + x); } } public static void main(String[] args) { Foo f = new Foo(23); Runnable b = f::bar; b.run(); // prints x = 23 }
- spion 9y agoI meant "bound", sorry. Funny that you mention "::" in Java. ES7/ES8 was going to have a bind operator "::" that extracts a bound method with a slightly different syntax. i.e. `::f.bar`
- xaedes 9y agoC++ also needs something like std::bind. To have to use some bind mechanism is reasonable for me. In JavaScript you could do this since quite some time with currying.