Sunday, June 17, 2018

JavaScript - built in method on Objects

Below code demonstrates the usage of built in methods and properties on Objects:

var person = {
  name: 'Sandra',
  age: 27
}

//deleting properties
// checks if the field name is present in the object person
console.log('name' in person);

//determine if they are there 
// deletes the field from the object
delete person.name;

console.log(person);



var school = {
  name: 'Sadhana Infinity International School',
  age: 1,
  place: "Nallagandla",
  greet: function () {
    return "Welcome to "+this.name;
  }
}

//demonstrates usage of for loop
// to access fields and values in Object
for (var field in school) {
  console.log(field +":"+school[field]);
}





Refer:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete

Practice at:
http://jsbin.com

No comments:

Post a Comment