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 "this" keyword refers to the closest defined object in scope. Consider the following example var person = { "firstName": "FirstName", "lastName": "LastName", "nestedObj": { "nestedObjectVar": "nestedObjectvariable", "nestedObjFunction": function () { return this.firstName + " " + this.nestedObjectVar; } } }; console.log(person.nestedObj.nestedObjFunction()); //prints undefined nestedObjectvariable because this.firstName is not within the nestedObj scope but this.nestedObjectVar is.
Georgi-I got confused after seeing your example. whenever this.firstname is executed if the firstname is not present within an object(in our case-nestedObj )why its not checking firstname in the outer object(person object) where firstname resides. someone please clarify
As he explains keyword "this" considers the recent object. It permits only for nearest object, won't consider outer r outer most objects. Default property of "this" keyword is utilising (considering r referring) the recent object only.
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?
Hi Koushik - Your way of teaching is awesome. Love it so much. Could you help me find the root cause of why the output of the following piece of code gives as: "Abhijit undefined"? NOTE- Here I have not altered (as done in your example) the definition of the "person" object. Also, I haven't assigned my current object to a new object in order to use the new object as you did here. Code-piece:- var person = { "firstName" : "Abhijit", "lastName" : "Mondal" }; person.getFirstName = function(){ console.log(person["firstName"]); }; console.log(person.getFirstName());
So person is set to empty, and if person2 is also pointing to person, shouldnt it still be undefined? unless person2 makes a new copy of person and points to it
Using "this" will not change anything. The reason person2 gets lost after loosing person is because they are objects and therefore relative type. Which means both variables point against one and the same object in memory. You can assign 100 variables in this way and they will all be pointing toward one object.
Great explanation of the keyword "this"; small note, at 8:51 you say "this" is a keyword in other languages and name "C" as one of them. This, (no pun intended) is not true.
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 :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.
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());
var fullName = person.getName; console.log(fullName()); ---------------------------------------------------- var fullName = person.getName(); console.log(fullName); Why above two ways give different output??
fullName() will go to the function and execute it and do what the function's instructions tell it to do, but the console.log(fullName) will literally print the function itself.
now I hope getName is a variable of function to execute a function you have initialise that function whenever you provide () parentheses it will execute code inside that function. else the function inside person object will be assigned to the fullName variable it won't execute that function.
The best explanation I got so far for this keyword
Hey thanks Koushik for this. Very lucid explanation as usual.
This is called Moksha (Salvation). Hats off Koushik.
thanks for the explanation and example Koushik! very well explained and easy to understand.
Outstanding explanation!
thank you your explanation made it so easy to understand
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.
holy balls this is amazing, thank you, I was so confused about how this actually works
wow that was the best explanation I have gotten for "this" Keep up the great work.. you are awesome
intelligent, real world example for "this".
Superb explanation
For the first time youtube suggested me the desired video for me
Thank you sir :)
this explanation was awesome :P
You should have seen net ninja explanation. It is much shorter
This
The best explanation!
Thanks for the "this" clear explanation.
This tutorial is awesome, you explain clearly
Thank you!!! The best explanation of this keyword
Really Really Really, I was looking for that. Nice explanation. Thanks.
Very well explained, thanks
Good explanation.
Great video man, thanks
Thank you for such an amazing explaination
Very good explanation
Thank you so much....very very easy to understand
great explanation.
What happens when I call "this" in a nested object/property? Would it return the closest parent obj or the parent of parents obj?
the "this" keyword refers to the closest defined object in scope. Consider the following example
var person = {
"firstName": "FirstName",
"lastName": "LastName",
"nestedObj": {
"nestedObjectVar": "nestedObjectvariable",
"nestedObjFunction": function ()
{
return this.firstName + " " + this.nestedObjectVar;
}
}
};
console.log(person.nestedObj.nestedObjFunction()); //prints undefined nestedObjectvariable because this.firstName is not within the nestedObj scope but this.nestedObjectVar is.
Georgi-I got confused after seeing your example.
whenever this.firstname is executed if the firstname is not present within an object(in our case-nestedObj )why its not checking firstname in the outer object(person object) where firstname resides.
someone please clarify
As he explains keyword "this" considers the recent object. It permits only for nearest object, won't consider outer r outer most objects.
Default property of "this" keyword is utilising (considering r referring) the recent object only.
That was great explanation thank you
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?
very nice explanation
Hi Koushik - Your way of teaching is awesome. Love it so much.
Could you help me find the root cause of why the output of the following piece of code gives as:
"Abhijit
undefined"?
NOTE- Here I have not altered (as done in your example) the definition of the "person" object. Also, I haven't assigned my current object to a new object in order to use the new object as you did here.
Code-piece:-
var person = {
"firstName" : "Abhijit",
"lastName" : "Mondal"
};
person.getFirstName = function(){
console.log(person["firstName"]);
};
console.log(person.getFirstName());
you need to put 'return person["firstName"];' in person.getFirstName function in order to print outside of the function.
Big thank you.
So person is set to empty, and if person2 is also pointing to person, shouldnt it still be undefined? unless person2 makes a new copy of person and points to it
They are objects, therefore relative types and so person and person2 point to the same object in memory. Your first guess is right.
Good explain
thanks a lot
Why is person undefined? Person isnt empty. I'm confused
Tenk you very much!
Using "this" will not change anything. The reason person2 gets lost after loosing person is because they are objects and therefore relative type. Which means both variables point against one and the same object in memory.
You can assign 100 variables in this way and they will all be pointing toward one object.
nice expleenation
simple and super
why are firstname and lastname in quotations?
Great explanation of the keyword "this"; small note, at 8:51 you say "this" is a keyword in other languages and name "C" as one of them. This, (no pun intended) is not true.
great description and great lesson,
but I don't understand person = {}; it should be removed
What do you not understand? Can you be a little more specific?
No, Focus.., person = {}; crush the previous declaration of
awesome
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 :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.
Slightly misleading title but a fantastic explaination anyway
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());
If you are trying to imitate the program as in video, you forgot this step:
employee = { };
var fullName = person.getName;
console.log(fullName());
----------------------------------------------------
var fullName = person.getName();
console.log(fullName);
Why above two ways give different output??
fullName() will go to the function and execute it and do what the function's instructions tell it to do, but the console.log(fullName) will literally print the function itself.
now I hope getName is a variable of function to execute a function you have initialise that function whenever you provide () parentheses it will execute code inside that function.
else the function inside person object will be assigned to the fullName variable it won't execute that function.
confused
Haha! you just forgot your last name.
Good explanation