JavaScript for Developers 38 - Understanding the this keyword

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ต.ค. 2024

ความคิดเห็น • 61

  • @jannathaysha
    @jannathaysha 4 ปีที่แล้ว

    The best explanation I got so far for this keyword

  • @dnyaneshtathode5511
    @dnyaneshtathode5511 2 ปีที่แล้ว

    Hey thanks Koushik for this. Very lucid explanation as usual.

  • @PrasoonNaik
    @PrasoonNaik 4 ปีที่แล้ว

    This is called Moksha (Salvation). Hats off Koushik.

  • @karranz
    @karranz 6 ปีที่แล้ว

    thanks for the explanation and example Koushik! very well explained and easy to understand.

  • @VishalRaoOnYouTube
    @VishalRaoOnYouTube 3 ปีที่แล้ว

    Outstanding explanation!

  • @kiratski47
    @kiratski47 7 ปีที่แล้ว +5

    thank you your explanation made it so easy to understand

  • @nickdhrones6425
    @nickdhrones6425 6 ปีที่แล้ว +4

    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.

  • @waterybenis5914
    @waterybenis5914 6 ปีที่แล้ว +2

    holy balls this is amazing, thank you, I was so confused about how this actually works

  • @rashmedo
    @rashmedo 5 ปีที่แล้ว

    wow that was the best explanation I have gotten for "this" Keep up the great work.. you are awesome

  • @samirhadim9367
    @samirhadim9367 6 ปีที่แล้ว

    intelligent, real world example for "this".

  • @bimalpariyar3732
    @bimalpariyar3732 5 ปีที่แล้ว

    Superb explanation
    For the first time youtube suggested me the desired video for me
    Thank you sir :)

  • @rohitsancheti96
    @rohitsancheti96 7 ปีที่แล้ว +18

    this explanation was awesome :P

    • @NeillHolloman
      @NeillHolloman 7 ปีที่แล้ว

      You should have seen net ninja explanation. It is much shorter

    • @zissler1
      @zissler1 4 ปีที่แล้ว

      This

  • @MrBenjjj6
    @MrBenjjj6 6 ปีที่แล้ว +2

    The best explanation!

  • @angelanorris546
    @angelanorris546 6 ปีที่แล้ว

    Thanks for the "this" clear explanation.

  • @albanustemu7396
    @albanustemu7396 6 ปีที่แล้ว

    This tutorial is awesome, you explain clearly

  • @sergiyrudenko905
    @sergiyrudenko905 5 ปีที่แล้ว

    Thank you!!! The best explanation of this keyword

  • @internettechnology86
    @internettechnology86 6 ปีที่แล้ว

    Really Really Really, I was looking for that. Nice explanation. Thanks.

  • @BigyanDhital
    @BigyanDhital 6 ปีที่แล้ว

    Very well explained, thanks

  • @aesean
    @aesean 4 ปีที่แล้ว

    Good explanation.

  • @tntdogs6910
    @tntdogs6910 5 ปีที่แล้ว

    Great video man, thanks

  • @priyankajadhav958
    @priyankajadhav958 6 ปีที่แล้ว

    Thank you for such an amazing explaination

  • @quincyelery5289
    @quincyelery5289 6 ปีที่แล้ว

    Very good explanation

  • @webprogramming2707
    @webprogramming2707 8 ปีที่แล้ว

    Thank you so much....very very easy to understand

  • @RishabhAgrawal804
    @RishabhAgrawal804 5 ปีที่แล้ว

    great explanation.

  • @ThuanNguyen-bo9ig
    @ThuanNguyen-bo9ig 8 ปีที่แล้ว +6

    What happens when I call "this" in a nested object/property? Would it return the closest parent obj or the parent of parents obj?

    • @gfdgdf28
      @gfdgdf28 7 ปีที่แล้ว +9

      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.

    • @praveen3123
      @praveen3123 7 ปีที่แล้ว +2

      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

    • @yuvakishoredommaraju1092
      @yuvakishoredommaraju1092 6 ปีที่แล้ว +3

      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.

  • @deepakmishra1875
    @deepakmishra1875 6 ปีที่แล้ว

    That was great explanation thank you

  • @saucy05
    @saucy05 6 ปีที่แล้ว

    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?

  • @TheAidsaids
    @TheAidsaids 6 ปีที่แล้ว

    very nice explanation

  • @abhijitmondal9203
    @abhijitmondal9203 5 ปีที่แล้ว

    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());

    • @ketankumar5689
      @ketankumar5689 2 ปีที่แล้ว

      you need to put 'return person["firstName"];' in person.getFirstName function in order to print outside of the function.

  • @johnbritolima
    @johnbritolima 7 ปีที่แล้ว

    Big thank you.

  • @muhammadarslanbhatti2139
    @muhammadarslanbhatti2139 4 ปีที่แล้ว

    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

    • @iliashterev38
      @iliashterev38 3 ปีที่แล้ว

      They are objects, therefore relative types and so person and person2 point to the same object in memory. Your first guess is right.

  • @coldtraveldiaries
    @coldtraveldiaries 6 ปีที่แล้ว

    Good explain

  • @kokomi5858
    @kokomi5858 5 ปีที่แล้ว

    thanks a lot

  • @nelsonjimenez7939
    @nelsonjimenez7939 5 ปีที่แล้ว

    Why is person undefined? Person isnt empty. I'm confused

  • @TheGuroguro12
    @TheGuroguro12 8 ปีที่แล้ว

    Tenk you very much!

  • @iliashterev38
    @iliashterev38 3 ปีที่แล้ว

    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.

  • @gopivadde8392
    @gopivadde8392 6 ปีที่แล้ว

    nice expleenation

  • @balajikj5342
    @balajikj5342 6 ปีที่แล้ว

    simple and super

  • @benwood9510
    @benwood9510 6 ปีที่แล้ว

    why are firstname and lastname in quotations?

  • @eric67361
    @eric67361 3 ปีที่แล้ว

    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.

  • @buyinga7mada222
    @buyinga7mada222 8 ปีที่แล้ว

    great description and great lesson,
    but I don't understand person = {}; it should be removed

    • @MrEternalFool
      @MrEternalFool 8 ปีที่แล้ว

      What do you not understand? Can you be a little more specific?

    • @samirhadim9367
      @samirhadim9367 6 ปีที่แล้ว

      No, Focus.., person = {}; crush the previous declaration of

  • @kim92se64
    @kim92se64 5 ปีที่แล้ว

    awesome

  • @sourishdutta9600
    @sourishdutta9600 6 ปีที่แล้ว

    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.

  • @PMPhotographyVideography
    @PMPhotographyVideography 6 ปีที่แล้ว

    Slightly misleading title but a fantastic explaination anyway

  • @awaiszaki14
    @awaiszaki14 6 ปีที่แล้ว

    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());

    • @werthero
      @werthero 6 ปีที่แล้ว

      If you are trying to imitate the program as in video, you forgot this step:
      employee = { };

  • @MegaBelltone
    @MegaBelltone 7 ปีที่แล้ว

    var fullName = person.getName;
    console.log(fullName());
    ----------------------------------------------------
    var fullName = person.getName();
    console.log(fullName);
    Why above two ways give different output??

    • @ramyalayan9121
      @ramyalayan9121 7 ปีที่แล้ว

      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.

    • @yuvakishoredommaraju1092
      @yuvakishoredommaraju1092 6 ปีที่แล้ว

      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.

  • @shane03ab
    @shane03ab 6 ปีที่แล้ว

    confused

  • @LokeshSharmaa
    @LokeshSharmaa 7 ปีที่แล้ว

    Haha! you just forgot your last name.

  • @0the0ambient0
    @0the0ambient0 7 ปีที่แล้ว +1

    Good explanation