Results: 1580
Notes
  • Newest first
  • Oldest first
  • Newest first(All)
  • Oldest first(All)
To find out if an object has a specific property as one of its own property. var school = {schoolName:"MIT"};


console.log(school.hasOwnProperty ("schoolName"));  // true because schoolName is an own property on the school object


// Prints false because the school object inherited the toString method from Object.prototype, therefore toString is not an own property of the school object.

console.log(school.hasOwnProperty ("toString"));  

by Luka Tatarishvili
4 years ago
0
JavaScript
objects
0
var person = {
    firstName: "Penelope",
    lastName: "Barrymore",
    fullName: function () {
        ​// Notice  "this"  refers to an object
        console.log(this.firstName + " " + this.lastName);
    ​// We could have also written this:​
        console.log(person.firstName + " " + person.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
   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",
    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
  // 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
"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
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
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
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
Results: 1580