Results: 1578
Notes
  • Newest first
  • Oldest first
  • Newest first(All)
  • Oldest first(All)
In HTML event handlers, this refers to the HTML element that received the event:
<button onclick="this.style.display='none'">
  Click to Remove Me!
</button>
by Luka Tatarishvili
4 years ago
0
JavaScript
objects
0
JavaScript strict mode does not allow default binding. So, when used in a function, in strict mode, this is undefined.

"use strict";
function myFunction() {
  return this;
}
// returns undefined because we have used strict which doesn't allow default mode
by Luka Tatarishvili
4 years ago
0
JavaScript
objects
0
in a function, this refers to the Global object [object Window].
function myFunction() {
  return this;
}
by Luka Tatarishvili
4 years ago
0
JavaScript
objects
0
When used alone, the owner is the Global object, so this refers to the Global object. In a browser window the Global object is
[object Window]:
var x = this;
x // [object Window]
by Luka Tatarishvili
4 years ago
0
JavaScript
objects
0
fullName : function() {
  return this.firstName + " " + this.lastName;
}
In an object method, this refers to the "owner" of the method.
The person object is the owner of the fullName method.
by Luka Tatarishvili
4 years ago
0
JavaScript
objects
0
"this" has different values depending on where it is used:
In a method, this refers to the owner object.
Alone, this refers to the global object.
In a function, this refers to the global object.
In a function, in strict mode, this is undefined.
In an event, this refers to the element that received the event.
Methods like call(), and apply() can refer this to any object.
by Luka Tatarishvili
4 years ago
0
JavaScript
objects
0
  // window is the object that all global variables and functions are defined on, hence:
    window.showFullName (); // Peter Ally

    // "this" inside the showFullName () method that is defined inside the person object still refers to the person object, hence:
    person.showFullName (); // Penelope Barrymore
by Luka Tatarishvili
4 years ago
0
JavaScript
objects
0
var person = {
    firstName   :"Penelope",
    lastName    :"Barrymore",
    showFullName:function () {
    // "this" on the line below refers to the person object, because the showFullName function will be invoked by person object.
    console.log (this.firstName + " " + this.lastName);
    }
    }

    showFullName (); // Peter Ally
    person.showFullName() // Penelope Barrymore
by Luka Tatarishvili
4 years ago
0
JavaScript
objects
0
   var firstName = "Peter",
    lastName = "Ally";

    function showFullName () {
    // "this" inside this function will have the value of the global variables
    // because the showFullName () function is defined in the global scope, just like the firstName and lastName
    console.log (this.firstName + " " + this.lastName);
    }
by Luka Tatarishvili
4 years ago
0
JavaScript
objects
0
var person = {
    firstName   :"Penelope",
    lastName    :"Barrymore",
    // Since the "this" keyword is used inside the showFullName method below, and the showFullName method is defined on the person object,
    // "this" will have the value of the person object because the person object will invoke showFullName ()
    showFullName:function () {
    console.log (this.firstName + " " + this.lastName);
    }

    }

    person.showFullName (); // Penelope Barrymore
by Luka Tatarishvili
4 years ago
0
JavaScript
objects
0
Results: 1578