10 The this keyword I


#1

Hi
The this keyword is supposed to access a value property outside the object but not in my code ? I don"t know why .

ERROR Message : Cannot read property name of undefined .
https://www.codecademy.com/courses/learn-javascript-objects/lessons/objects/exercises/this?action=lesson_resume&course_redirect=introduction-to-javascript
thanks !


#2

The author wants us to see that arrow function notation does not recognize the this context variable. It is an object of the normal function or method.

sayHello () {
    return `... ${this.name}`;
}

Arrow functions do not have this or arguments objects. They are intended as pure functions.


#3

It works ! Thanks a lot Roy .
… but why do we use the THIS as we are inside the object , so name would be enough , wouldn’t it ?
Name is the scope of name , so we shouldn’t have tio use this .
I thought the THIS was necessary when we are out of the value’s scope , ie outside the object , wasn’t it ?
Thanks Roy for your explanation !


#4

Inside an object we never use a dedicated identifier, else the object cannot be cloned or renamed without editing the inner code. this always refers to the current object for an execution context.

Are you referring to instances of the object as being outside? this can only ever apply to execution context inside the object. It has no definition in global scope. this in global scope would refer to the window object.


#5

thanks Roy !

Blockquote


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.