Access the full course here: https://javabrains.io/courses/corejs_jsfordev
Learn how to access the ‘self’ reference on the object by using the `this` keyword.
Original source
Access the full course here: https://javabrains.io/courses/corejs_jsfordev
Learn how to access the ‘self’ reference on the object by using the `this` keyword.
Original source
40 responses to “JavaScript for Developers 38 – Understanding the this keyword”
Very good explanation, as always.
So the this. keyword ignores the object name(variable name)?
Why we using "getFullName" instead of just saying "fullName"?
I think it's important to point out that:
The KEY values in the properties within the object such as "firstName": ….are in quotations. Previously taught that only reserved words and spaces had to be quoted but JSON requires ALL KEYS to be quoted(from what I understand, I had to look it up).
Thank you for such an amazing explaination
In his other lesson about objects, he mentioned the "this" object is created and returned inside an instance of an object constructor when we use the "new" keyword.
Where is the "this" object in this example? Do objects always have "this" object within them?
var student = {
name : "Shoaib",
sayname : function() {
console.log(this);
var abjqdq =5;
var bar = function() {
console.log(this);
}
bar()
}
}
student.sayname();
console.log(bar);
console.log(abjqdq);
VM504:4 {name: "Shoaib", sayname: ƒ}
VM504:7 Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, frames: Window, …}
VM504:14 () => {
console.log(this.name);
}
VM504:15 Uncaught ReferenceError: abjqdq is not defined
at <anonymous>:15:13
(anonymous) @ VM504:15
can You please explain to me why this function bar is getting the global scope and if the bar is getting global scope then why (abjqdq ) variable is getting the local scope of that method (sayname). And also why the bar (function) is getting the window context when it is called into the sayname object.Appreciate your hard work.Thank You so much. Please let me know the reason behind this.
holy balls this is amazing, thank you, I was so confused about how this actually works
That was great explanation thank you
Really Really Really, I was looking for that. Nice explanation. Thanks.
nice expleenation
why are firstname and lastname in quotations?
Slightly misleading title but a fantastic explaination anyway
confused
intelligent, real world example for "this".
Very well explained, thanks
simple and super
Thanks for the "this" clear explanation.
Very good explanation
This tutorial is awesome, you explain clearly
Good explain
I love this coarse. My only concern is the constant use of JSON notation in you object literals.
Specifically the use of quoted strings for property names in the object literals. This is required for JSON, but is very unnecessary for objects . In my opinion, the lesson code would be more readable if you would not quote the property names.
The best explanation!
thanks for the explanation and example Koushik! very well explained and easy to understand.
very nice explanation
why below this code print all property without this keyword? can any one explain
var employee = {
"empFirstName": "Ashan",
"empLastName": "Karim",
"getEmployeeFullName": function(){
return employee.empFirstName + " " + employee.empLastName;
}
};
var fullName = employee.getEmployeeFullName();
var employee1 = employee;
console.log(employee1.getEmployeeFullName());
thank you your explanation made it so easy to understand
var fullName = person.getName;
console.log(fullName());
—————————————————-
var fullName = person.getName();
console.log(fullName);
Why above two ways give different output??
Good explanation
You are misleading. The this keyword in javascript not always pointing to the "self" object. Sometimes we need to bind self to this. This video isn't good, you are teaching wrong coding methods of javascript
I still don't understand the drastic length of explaining the keyword 'this'. It has a very simple context. "Understanding the this keyword" would've meant more of a video title if the video explained the scopes of this, and other far more complex mechanics of the keyword.
Big thank you.
how that so relaxing now I finally understand what that this means again ^^
this explanation was awesome 😛
AWESOME JOB MAN!!! KEEP IT UP!!!
Haha! you just forgot your last name.
Thank you so much….very very easy to understand
Tenk you very much!
good and neat explanation!! Understood what is this "this"!!!!!!
great description and great lesson,
but I don't understand person = {}; it should be removed