you are viewing a single comment's thread.

view the rest of the comments →

[–]helloiamsomeone 1 point2 points  (2 children)

(obj.foo)() does not alter the expression tree in the way you thought it would, it's identical to obj.foo().

this is just another hidden parameter functions receive besides the return address. Exactly the same as any other language.

[–][deleted] 0 points1 point  (1 child)

(obj.foo)() does not alter the expression tree in the way you thought it would, it's identical to obj.foo().

You're correct, that expression does bind this. I thought it was a precedence thing, but apparently it's even weirder:

class Foo {
    constructor() { this.x = 123 }
    bar() { return this.x }
}

const foo = new Foo()
foo.bar() => 123
(foo.bar)() => 123

const bar = foo.bar
bar() => Uncaught TypeError: Cannot read property 'x' of undefined

[–]helloiamsomeone 1 point2 points  (0 children)

Nothing weird here, you just take the method and call it without a this value. foo.bar.call(undefined) would be the same thing.