Javascript Context Tutorial - What makes Javascript Weird...and Awesome Pt5

LearnCode.academy · 96K views · 2014 · 13:59

Open on YouTube

Python, Ruby & scripting languagesWeb developmenttutorial

Why Stack Overflow recommends it

Stack Overflow users link it when answering questions about what `this` refers to in JavaScript callbacks and event handlers.

Recommended by 3 people across 3 different months, 2014 to 2020.

What it is

A 14-minute JavaScript tutorial from LearnCode.academy, part of a series on what makes JavaScript quirky. It explains how execution context and the value of `this` are determined, and how call, apply and bind change it, with a focus on callbacks.

What people said

Recommends kjsebastian · 2014-07 · Stack Overflow · on “How can I use id and value of text input?”

var productID = $(this).attr('id'); var qty = $(this).val(); This [this video] video better illustrates the use of this and contexts in javascript

Recommends Rajesh Dixit · 2015-10 · Stack Overflow · on “Working of This keyword in javascript”

You can watch [this video] . But to put it simple words, `this` represunts current object.So when you do `myCar.getCar()` current object is *myCar* but when you do `getCar()`, as mentioned be @Alnitak, since there is no object in LHS, default object is window.

Recommends Tony · 2020-07 · Stack Overflow · on “How to change data in Vue JS dynamically”

like @punund said, this here does not reference the component to which the data property is attached. The context in which you are using the this keyword is in the callback. There are a couple of things you can do. You can use a variable to save a reference to this const self = this; axios.get('/accounts/passwordresetpage_ajax_validation/' + '?email=' + email.value) .then(function (res) { if (res.data.email_does_not_exists) { self.showError = true } else { document.querySelector('form').submit() } }); You can also use the bind method to return a function that has the context of the vue component axios.get('/accounts/passwordresetpage_ajax_validation/' + '?email=' + email.value) .then(functio