JavaScript this Keyword




JavaScript this Keyword

🔥Get the COMPLETE course (83% OFF – LIMITED TIME ONLY): http://bit.ly/2M1sp4B

Subscribe for more videos:
https://www.youtube.com/channel/UCWv7vMbMWH4-V0ZXdmDpPBA?sub_confirmation=1

Want to learn more from me? Check out my blog and courses:

http://programmingwithmosh.com
https://www.facebook.com/programmingwithmosh/


Original source


37 responses to “JavaScript this Keyword”

  1. Very nice Mosh. Now why there are dozens of complicated articles that doesnt make sense? I read today abou five long articles about call(), apply(), bind() and this but was more and more confused. They maybe get it right but teach it wrong … in this short video I had two big insights – it clicked all together. Thx.

  2. Urgent Question: I am fairly comfortable with JavaScript. I know ES6 basics as well useful for React developers.
    I have an interview in 4-5 days and wanna know should I invest my time in Mosh's JavaScript YouTube video or the course?
    Is it beneficial for a JavaScript focussed interview with tech giants here in the US?

  3. Honestly, this makes so much sense to me. When people were telling me the 'this' in JavaScript is complicated I was worried that I would struggle to learn it but it makes so much sense if you stick to the rules for methods, functions, and constructor functions.

  4. At first i didn't understand this, then my brain got fucked by the explanation but afterwards i understood, as an overall it was an incredible explanation you just got a let it sink.

  5. You can also do like this:

    const video = {
    title: 'a',
    tags: ['a','b','c'],
    showTags(){
    this.tags.forEach((tag) => {
    console.log(this.title, tag);
    });
    }
    };

    video.showTags();

    the => does not create a "this" context, it gets the "this" context from the enclosing scope, which in this case, is the video object itself. Nice lesson, I learned a lot.

  6. tl;dr:
    case 1:
    `this` in an object method — referencing the object.
    case 2:
    `this` in a regular function — referencing `window` (`global` in node.js)

    case 3 – a special case inferred from case 1:
    function Video() {console.log(this);}
    new Video;
    referencing a new `Video` object, because `new` creates an empty object `{ }` and `Video` serves as a constructor method of the object.
    case 4 – a special case inferred from case 2:
    `this` in a regular function in an obj method — referencing `window` because `this` is in a regular function.

    case 1 at 1:00
    case 2 at 2:13
    case 3 at 2:33
    case 4 at 4:35

    Note: all the four cases above are based on the assumption that nothing is tampered with by things like `call`, `apply` and `bind`.

Leave a Reply