JavaScript "Advanced Objects" Ex 3 "Arrow Functions and this


#1

I need help. I don’t get what it’s referring to when it says this is the global object??
Can anybody help , much appreciated thank you.

const goat = {
dietType: ‘herbivore’,
makeSound(){
console.log(‘baaa’);
},
diet: () => {
console.log(this.dietType)
}
};

goat.diet(); // Prints undefined

In the code snippet above, the value of this is the global object , or an object that exists in the global scope


#2

if we look at method definition:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions

we see that there are two ways to define methods, you don’t use either of the two possible options. As such this refers to the global window object, and doesn’t properly bind to the object.


#3

hi < thank you so much. Do you work for Codecademy? I am using arrow function syntax with “this”? to print the property “dietType” ?


#4

In the code snippet above, the value of this is the global object, or an object that exists in the global scope, ??? I know what Global Scope is , i learnt that when dealing with variables. I don’t understand this because this hasn’t got a value?? Has it? i thought that was a property / key you’re attaching “this” to?


#5

Okay after reading it all again , i get it! When this keyword in a method then the value of this is the calling object. However, this part “the value of this is the global object , or an object that exists in the global scope”,

i don’t understand because there isn’t a Global Object? There is only a 1 method inside an 1 Object. Or am i going crazy? o what is this Global Object it’s referring to? Can anybody help?


#6

sorry need to write that again. So after reading it all again , i get it! When this keyword is used in a method then the value of this is the calling object. I get that! However, this part … “the value of this is the global object , or an object that exists in the global scope”,

I don’t understand because there isn’t a Global Object? Or anotherObject that exissts in the global scope! I am literally looking at 1 method inside 1 Object. Or am i going crazy? So what is this Global Object it’s referring to? Can anybody help?

much appreciated!


#7

The global object is window. Arrow functions do not have a this object so the interpreter looks up the scope chain and finds it on the window object. Since there is no diet property for that object, it comes back as undefined.

If you want instance context, then use method syntax.

diet () {
  console.log(this.dietType);
}

#8

hello mtf, i have seen you on google many times when i have put in a question related to coding. You’re a popular man lol. Okay thank you, i have no idea and have never came across “window” so i have absolutely no idea what that is BUT i have some docs in front of me about it so let me read about it and i will get back to you guys. Hold that fort ! and thanks


#9

thanks stetem94. Gona do a bit of reading regarding “window”. Thanks for your reply


#10

Consider,

var foo = 'foo';

console.log(window.foo);                    // foo

All global variables are properties of the window object.

console.log(window.hasOwnProperty(foo));     // true

#11

so you’re saying there’s like an invisible default Object called “window”


#12


#13

came back as “window” is not defined.


#14

its not invisible, but given how closely JS and webpages are related, window is quite logic

maybe a codecademy restriction, works fine on jsbin:

http://jsbin.com/zipuqizeje/edit?js,console,output

but lets not get off-topic too much, have you already properly declared your method?


#15

Try in your JS console…

 > foo = 'foo'
<- "foo"
 > window.foo
<- "foo"
 > window.hasOwnProperty(foo)
<- true

#16

I gota go out now. Be back tom morning. But please leave your messages. I will read when i get back in. Thanks


#17

hi stetim , i really need your help. I am still not understanding it , the docs are confusing and i have been stuck on the definition of Object “Window” for a day and a half now so i cannot move on until i get what it is


#18

these example don’t work in the codecademy editor, they all come back with an error!


#19
/home/ccuser/workspace/javascript_101_Unit_3_v2/pizza.js:2
console.log(window.foo);
            ^

ReferenceError: window is not defined

Above I picked a workspace at random… It doesn’t work because it may be blocked to protect the namespace.

Open the console of your own browser. Each line you see above beside a prompt, >, is entered and a response <- comes immediately. In Chrome it appears even before we press Enter.


#20

okay im gona ask one more question. And this should get me the answer. I got this off the MDN doc.
it says:

“In JavaScript, there’s always a global object defined.”

So my question again IS … where is this global object??? I only see ONE object in the example diagram

in the narrative on the left of the page!