ga-wdi-boston / js-function-context-this

Other
0 stars 126 forks source link

Confusing info #39

Open raq929 opened 7 years ago

raq929 commented 7 years ago
Is the function called with a context (implicit binding), otherwise known as an owning or containing object? If so, this is that context object. let bar = obj1.foo()

The owning or containing object IS NOT NECESSARILY the context. You can call it in a different context using obj1.foo().call(someOtherObject).

I'm not quite sure how to reword this. It's not WRONG, but many people get confused and think that because an object contains a function, this is set to that object when it is written, not at runtime, and I want to be extremely clear about how that's not the case.

jrhorn424 commented 7 years ago

obj1.foo().call(someOtherObject)

That's an explicit binding, which is not what the quoted text is referring to.

Is the function called with a context (implicit binding)

I think the parenthetical is important. It should be a parenthetical.

I'm not sure when to re-emphasize that this is not determined until the function is called. It's one of those things, like the definition of a callback, that I drill into them when I deliver this talk.

jrhorn424 commented 7 years ago

Binding isn't a very good word here, either.

raq929 commented 7 years ago

Yeah. I drilled it too. Maybe that has to be enough?

gaand commented 7 years ago

Could we add

foo.call( obj2 ) // this === obj2
obj1.foo() // this === obj1

Also, could we not use the name foo in different ways in this list (uncapitalized constructor function, method - key lookup in an object, a regular function, yada)?

MicFin commented 7 years ago

After discussing with @gaand, the example would be more appropriately shown as:

obj1.foo() // this === obj1
obj1.foo.call( obj2 ) // this === obj2