JavaScript for Developers 38 – Understanding the this keyword




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”

  1. 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).

  2. 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?

  3. 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.

  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.

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

  6. 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

  7. 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.

Leave a Reply