Wednesday, June 13, 2018

JavaScript - this keyword - usage within JSON

Below code demonstrates the usage of this key keyword within JSON:

// declaring a javascript object
var person = {
  name: "MS Dhoni",
  "firstName": "Mahendra",
  city: "Ranchi",
  team: "Chennai Super Kings",
  details: {
    hobbies: ["cooking","cricket","chess"],
    location: "India"
  },
  greeting: function() {
  console.log("welcome "+this.name);
}
}

// Printing the person object to console
//console.log(person);



// Printing the person name to console
console.log(person.name);

person.name = "Donald";

// Printing the person name to console
console.log(person.name);

person.greeting();





Refer: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this
Practice at: http://jsbin.com


No comments:

Post a Comment