Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialDominik Kaczmarczyk
19,509 Points"this" meaning in javascript anonymous functions and more
Hello, I've read a lot about arrow and standard functions syntax in javascript, but theres still couple of things that confuse me.
- What is "this" in anonymous functions bound to?
Lets say we are building a react app and we've got something like this:
class App extends Component {
render() {
return(
<button onClick={()=>{console.log(this)}}/>
)};
}
Console output after render: <App>
I know that arrow function "this" is referring to object where arrow function was declared. But what happens if we use React Bootstrap for example and we use it's <Button> component and we'll pass anonymous arrow function, returning console.log(this), to onClick event of that Bootstrap <Button/> ? Is "this" bound to <App> or <Button> (now its component, not html element) now and why? So first thing, what's the "declaration scope" while declaring an anonymous function, and is it when declaring it inside <button> and <Button> elements?
Another thing that confuses me is "normal" function's "this" in some cases.
My understanding is that if we call standard-syntax function returning console.log(this) directly (not with dot before ) in the browser , "window" will be logged. But when I'll run above react example with standard function instead of arrow one "undefined" is logged instead of Window. Why?Another case. lets say we are invoking someObject.function1() function. Now "this" in function1() body is reffering to the someObject. But what happens if we declare and execute another standard-syntax function2 which is using "this" keyword inside function1() body? Example:
obj = {
function1 = function() {
console.log(this);
function function2() {
console.log(this);
}
function2();
}
}
obj.function1();
I'm not looking for just one word result answer- but more like for the "general rule". Is "this" in this nested function out of our control now? What if the function2 would be an arrow function? Would it take "this" of scope that it was declared in (function1() scope, so "this" would be reffering to someObject)? I wondered if "this" is passed only to the "first" child function or the whole scope.
Most tutorials cover only those obvious cases, not including chaining, nested functions etc. And I feel like I'll be lame developer if I won't master Javascript's "this".
So I would be really grateful for any explanation (preferrably better than just yes/no answers :D I could check yes/no in console, but I feel like even clear log would be just one case, and without "bigger" picture it means nothing) of cases I mentioned or some SOLID article links. Treehouse video on "this" covered only pretty basic usage too.
Thanks!
//Sorry for 1 & 1 & 2 question numbers, don't know why but Treehouse formatted this question like that, it was 1 & 2 & 3.
1 Answer
Steven Parker
231,184 PointsThe binding of "this" is no different between anonymous or named functions. But as I said in your other question, arrow functions do not create their own bindings to the "this" like conventional functions do. MDN suggests that "Arrow function expressions are ill suited as methods".
For more detains, see the MDN page on Arrow functions.
And Markdown always re-numbers ordered lists, so it must have thought you were starting a separate list.