I didn’t find the example on Codecademy the easiest to understand.
The TL;DR is why ‘this’ in an arrow function won’t return the value is because of a JavaScript quick and ‘this’ when used inside the arrow function is now bound to the Global Object (where your global variables are) and NOT the object where the function is.
Hope this helps others who may have been a bit confused by this.
Arrow functions inherently bind , or tie, an already defined this value to the function itself that is NOT the calling object. In the code snippet above, the value of this is the global object , or an object that exists in the global scope, which doesn’t have a dietType property and therefore returns undefined .
From what I understand from the explanation given is that when this keyword is used in an arrow function it will call on the owner of the function which I believe is the object correct? But I dont know what is meant by object is it the object ‘robot’ in that case would it not call on robot???
classes/objects can be a good solution once your code base starts to grow, but first you need to learn classes/objects, and on a small program, classes/objects might not to seem to add a lot of value.
well, the quickest way is a buying a pre-made template/theme.
Being able to build a website quickly takes time and practice, first you need to know the basics of html and css. Once you are proficient with these, you can choice a css framework like bootstrap to speed up development.
Should the following statement say ‘local object’ instead of ‘global object’? In the code snippet above, the value of this is the global object , or an object that exists in the global scope, which doesn’t have a dietType property and therefore returns undefined . In my understanding the global object is defined outside of the function declaration. In this case would be it would “goat”. Inside ‘goat’ declaration its property “diet” declared the function: () => {
console.log(this.dietType); so property ‘dietType’ is inside the declaration also. I know I am most likely wrong but it makes sense why “this” does not work here based off my statement.