Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,016 members, 7,817,997 topics. Date: Sunday, 05 May 2024 at 02:58 AM

How Can I Understand The 'this' Keyword In Javascript - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / How Can I Understand The 'this' Keyword In Javascript (663 Views)

Javascripters: Telegram Group To All Interested In Javascript And Its Ecosystem / How To Retrieve Data From An Api Using Fetch Api In Javascript / How Do I Convert Object To Arrey In Javascript (2) (3) (4)

(1) (Reply) (Go Down)

How Can I Understand The 'this' Keyword In Javascript by Winningbot: 4:16pm On Jul 18, 2022
Honestly, I have dedicated almost a full day for this but I'm yet to understand it.

What's it's function in JavaScript

Can some spare few minutes to explain this for me please,

Please someone should help me
Re: How Can I Understand The 'this' Keyword In Javascript by Nobody: 4:26pm On Jul 18, 2022
So far.... what do u understand about it and what do u think it does since uv spent a day
Re: How Can I Understand The 'this' Keyword In Javascript by jbreezy: 4:48pm On Jul 18, 2022
Winningbot:
Honestly, I have dedicated almost a full day for this but I'm yet to understand it.

What's it's function in JavaScript

Can some spare few minutes to explain this for me please,

Please someone should help me
this refers to an object. E.g const Car = {
model:"toyota",
printModel: function(){console.log(this.model)
}
The this.model clearly indicates that it is an attribute of Car, hence when the printModel() method is invoked, the model of Car which is "toyota" is going to be logged. But be informed that if "this" is used outside an object variable, it will to the window itself. For more in-depth clarification, I will advice you to read "Mastering Javascript OOP" by Andrei Chielli or "Eloquent by Javascript", assuming you already have idea on basics and OOP doesn't take a full day to understand. It's quite broad and tricky.

1 Like

Re: How Can I Understand The 'this' Keyword In Javascript by LikeAking: 7:10pm On Jul 18, 2022
Na only this key word?

Many others dey wey go torture u.

That's part of the leaning process.
Re: How Can I Understand The 'this' Keyword In Javascript by Csami(m): 7:38pm On Jul 18, 2022
In a function aka a method, you can use “this” to refer to a property in an object.

Example
const dog = {
//property : value
name: “husky”,
legs: 4,
sentence: function(){
return this.name + “ has “ + this.legs + “ legs”;
//same as saying
return dog.name + “ has “ + dog.legs + “ legs”;

//husky has 4 legs
}
}

console.log(dog.sentence())

1 Like

Re: How Can I Understand The 'this' Keyword In Javascript by Nobody: 9:09pm On Jul 18, 2022
Winningbot:
Honestly, I have dedicated almost a full day for this but I'm yet to understand it.

What's it's function in JavaScript

Can some spare few minutes to explain this for me please,

Please someone should help me

*this" refers to the current object which could be an object, a HTML element, or the window

I'd recommend every Dev beginner especially to have w3schools locally
Re: How Can I Understand The 'this' Keyword In Javascript by Nobody: 9:15pm On Jul 18, 2022
LikeAking:
Na only did key word?

Many others dey wey go torture u.

That's part of the leaning process.


I swr JS syntax is just too broad, I've been coding is for more than 8 months and I still don't fully understand switch statements, id rather use long if-else grin .
Even OOP sef I don't like class, I'd rather use function object constructor. In fact I hardly use ES6 in general untill I found out that you actually need it for React angry undecided

2 Likes

Re: How Can I Understand The 'this' Keyword In Javascript by etoluw: 6:57am On Jul 19, 2022
Winningbot:
Honestly, I have dedicated almost a full day for this but I'm yet to understand it.

What's it's function in JavaScript

Can some spare few minutes to explain this for me please,

Please someone should help me

You can message me on whatsapp, i will give you full explanation on "this" in OBJECTS.

You'll also have to know about "bind", "call" & "bind"
Re: How Can I Understand The 'this' Keyword In Javascript by captainbangz: 7:44am On Jul 19, 2022
Think4Myself:


I swr JS syntax is just too broad, I've been coding is for more than 8 months and I still don't fully understand switch statements, id rather use long if-else grin .
Even OOP sef I don't like class, I'd rather use function object constructor. In fact I hardly use ES6 in general untill I found out that you actually need it for React angry undecided
My problem is "Promise"...Does it work same way try and catch does?
Re: How Can I Understand The 'this' Keyword In Javascript by bularuz(m): 10:16am On Jul 19, 2022
"this"
Re: How Can I Understand The 'this' Keyword In Javascript by tollyboy5(m): 11:37am On Jul 19, 2022
It's been long I worked with JavaScript. But this keyword is in many programming language and I first learnt about it in C++ but make use of it mostly now in PHP.

When you have a CLASS, you can call the CLASS method outside of the CLASS using the CLASS instance.

Maybe something like
Example: php

Class Car {
Public $carkey= " ";

Public function startEngine($key){
$this->carkey = $key;
}
};

$camry= new Car ();

As you can see from the CLASS Car there is a method to start the car engine. I need the car key �️ to start the car engine. But the variable $carkey containst an empty string for now.
Inother to start the car engine from inside the method I need to access the $carkey outside the method, let's not forget that the $carkey belongs to the CLASS Car.

If you access the $carkey inside the method without referencing it to it's CLASS, it will act as a new variable that has nothing to do with the $carkey variable belonging to the CLASS.

So how do we reference the $carkey variable that is outside the method to work inside the method "startEngine ()" ?
That's where the $this keyword comes in.

"$this->carkey " is trying to tell us that the carkey belongs to the CLASS Car that's all.

If we were to call the same $carkey outside the CLASS we can't use $this keyword nomore, what we'll use is a CLASS instance. Which is

" $camry= new Car (); " then we can now access it with

$camry->carkey = "on";

We can still put it off with the method if we like

$camry->startEngine('off');


So $this keyword replace instance of a CLASS when been used inside of the CLASS.

Try use this php basic example to understand further.

1 Like

Re: How Can I Understand The 'this' Keyword In Javascript by chukwuebuka65(m): 12:08pm On Jul 19, 2022
Winningbot:
Honestly, I have dedicated almost a full day for this but I'm yet to understand it.

What's it's function in JavaScript

Can some spare few minutes to explain this for me please,

Please someone should help me


“This” inside an object refers to that object it is inside of. It is mostly used inside functions in an object or class to refer to the properties of the class or object it is inside of. Eg.
const car = {
Model: “Toyota”,
Color:”red”,
Details: function ( ) {
return “my” + this.model + “is “ +
this.color
}
}
If i do, car.details( ) ; i will get “ my Toyota is red”.
But u can make “this” to refer to another object ( which may not have a “detail” property ) by using bind() or apply() or call(). eg .
const vehicle = {
Model: “corolla”,
Color:”black”,
}

If i do: car.details.bind( vehicle). I will get “my Corolla is black”. Because “this” is now referring to the object “vehicle” because I bind it with “vehicle”. Is quite simple.
NB. if you define your function inside the object using arrow functions, you cant use the bind(), apply() 0r call() method on it. "Thi"s will always refer to the car object in this case

2 Likes

Re: How Can I Understand The 'this' Keyword In Javascript by Enesth(f): 2:51pm On Jul 19, 2022
chukwuebuka65:



“This” inside an object refers to that object it is inside of. It is mostly used inside functions in an object or class to refer to the properties of the class or object it is inside of. Eg.
const car = {
Model: “Toyota”,
Color:”red”,
Details: ( ) => return “my” + this.model + “is “ +
this.color
}
If i do, car.details( ) ; i will get “ my Toyota is red”.
But u can make “this” to refer to another object ( which may not have a “detail” property ) by using bind() or apply() or call(). eg .
const vehicle = {
Model: “corolla”,
Color:”black”,
}

If i do: car.details.bind( vehicle). I will get “my Corolla is black”. Because “this” is now referring to the object “vehicle” because I bind it with “vehicle”. Is quite simple

This is very clear
Re: How Can I Understand The 'this' Keyword In Javascript by qtguru(m): 3:27pm On Jul 19, 2022
Winningbot:
Honestly, I have dedicated almost a full day for this but I'm yet to understand it.

What's it's function in JavaScript

Can some spare few minutes to explain this for me please,

Please someone should help me

Depending on where you are , the context for this can alter e.g

a onclick=function { this} //this points to the DOM Node

in an Object, this points to the object, in a function in an Object, this points to the Object

but in a function within a function and this points to the inner function regardless of being in an object or not.

this simply mean context, in Programming you enter a function, you are in that function's context.

This means that whatever is executing JavaScript C++ or Java, it will refer to variables within this context in this case function. However you can call a method in JS in another context using call. Prior to classes.

You could copy Object A's constructor in Object B, and from Object B method call Object A and change the context to point to Object B, so the code will refer to variables from Object B and Inheritance in JavaScript was born.

Read an article on it, this knowledge works in all programming languages.

Something like that, someone can correct me.
Re: How Can I Understand The 'this' Keyword In Javascript by shinemediaweb: 5:00pm On Jul 19, 2022
jbreezy:
this refers to an object. E.g const Car = {
model:"toyota",
printModel: ()=>console.log(this.model)
}
The this.model clearly indicates that it is an attribute of Car, hence when the printModel() method is invoked, the model of Car which is "toyota" is going to be logged. But be informed that if "this" is used outside an object variable, it will to the window itself. For more in-depth clarification, I will advice you to read "Mastering Javascript OOP" by Andrei Chielli or "Eloquent by Javascript", assuming you already have idea on basics and OOP doesn't take a full day to understand. It's quite broad and tricky.

chukwuebuka65:

“This” inside an object refers to that object it is inside of. It is mostly used inside functions in an object or class to refer to the properties of the class or object it is inside of. Eg.
const car = {
Model: “Toyota”,
Color:”red”,
Details: ( ) => return “my” + this.model + “is “ +
this.color

}
If i do, car.details( ) ; i will get “ my Toyota is red”.
But u can make “this” to refer to another object ( which may not have a “detail” property ) by using bind() or apply() or call(). eg .
const vehicle = {
Model: “corolla”,
Color:”black”,
}

If i do: car.details.bind( vehicle). I will get “my Corolla is black”. Because “this” is now referring to the object “vehicle” because I bind it with “vehicle”. Is quite simple



Broses error dey your codes
Check the bolded text - when you use arrow function for a method you do not capture this

2 Likes

Re: How Can I Understand The 'this' Keyword In Javascript by Nobody: 5:26pm On Jul 19, 2022
chukwuebuka65:



“This” inside an object refers to that object it is inside of. It is mostly used inside functions in an object or class to refer to the properties of the class or object it is inside of. Eg.
const car = {
Model: “Toyota”,
Color:”red”,
Details: ( ) => return “my” + this.model + “is “ +
this.color
}
If i do, car.details( ) ; i will get “ my Toyota is red”.
But u can make “this” to refer to another object ( which may not have a “detail” property ) by using bind() or apply() or call(). eg .
const vehicle = {
Model: “corolla”,
Color:”black”,
}

If i do: car.details.bind( vehicle). I will get “my Corolla is black”. Because “this” is now referring to the object “vehicle” because I bind it with “vehicle”. Is quite simple

"This" doesn't work on arrow function objects
Re: How Can I Understand The 'this' Keyword In Javascript by chukwuebuka65(m): 6:45pm On Jul 19, 2022
shinemediaweb:






Broses error dey your codes
Check the bolded text - when you use arrow function for a method you do not capture this

my bad . updated
Re: How Can I Understand The 'this' Keyword In Javascript by chukwuebuka65(m): 6:53pm On Jul 19, 2022
Think4Myself:


"This" doesn't work on arrow function objects

yea just noticed
Re: How Can I Understand The 'this' Keyword In Javascript by jbreezy: 8:07pm On Jul 19, 2022
shinemediaweb:






Broses error dey your codes
Check the bolded text - when you use arrow function for a method you do not capture this
true..I forgot. It's not that it doesn't work tho, but it become chained to the method in which it is being used.

(1) (Reply)

Website With Mail Facility Needed Urgently / Are There Really Good Website Developers In Nigeria? / Any Delphi Programmer In The House?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 46
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.