Objects Property in One Shot

Objects Property in One Shot

In this Article we will learn about the objects and its property in depth .

Objects :

Objects are used to store keyed collections of various data and more complex entitites . In Javascript everything is objects .

In objects the data are stored in the heap memory and reference is store in the stack memory . so we can increase or decrese size of the object .

An Object can be created with figure brackets {…} with an optional list of properites . A property is a key value pair , where key is string and value can be any thing .

An empty object can be created using one of two syntaxes :

let user = new Object() // Object constructor syntax 
let user = {} // Object literal syntax 

// we can write down properties in it 
let user = {
name:"alok",
age : 30,
}

Properties in Objects :

  1. Adding a new Property in Objects :
let user = {
name : "alok",
age:30,
} 

// Adding a property using defineProperty(objectName,"propery-name",{value:"value"})
Object.defineProperty(user,"year",{value:"2008"});

// directly 
user.year=2000;
  1. Deleting a property from the objects
let user = {
name : "alok",
age:30,
}

// using delete The delete operator removes a property from an object.
// If the property's value is an object and there are no more references to the object,
// the object held by that property is eventually released automatically. 
delete.user.name;
  1. Checking if the objects have property named
/*
Using hasOwnProperty()
The hasOwnProperty method of Objec returns a boolean indicating whether this object has the specified
property or not 
*/
let user = {
name : "alok",
age:30,
}
console.log(user.hasOwnProperty("name");//true

/*
using in operator 
The in operator also return boolean indicating whether this object has the specified property or not 

*/
console.log("name"in user) // true

/*
what is the differnce : 
The key difference is that in will return true for inherited properties, whereas hasOwnProperty() will return false for inherited properties.

For example, the Object base class in JavaScript has a __proto__ property, a constructor property, and a hasOwnProperty function. 
The in operator will return true for these properties, but hasOwnProperty() will return false.
// read more : https://masteringjs.io/tutorials/fundamentals/hasownproperty
*/

4 ) For .. in loop

To walk over the all keys of an object , there exist a special form of the loop : for … in . This is a completely

different thing from the for() .

let user = {
name:"alok",
age:30,
isMarried:false
}

for(let key in user ){
alert(key);// name ,age , isMarried 
alert(user[key]) // // alok  30 ,true
}

5) Ordered like an object :

Are objects ordered ? In other words ,if we loop over an object , dow we get all propeties in the same order they were added ?

The answer is ordered in special fashion ; integer properties are sorted . other appear in creation orer.

let codes = {
  "49": "Germany",
  "41": "Switzerland",
  "44": "Great Britain",
  // ..,
  "1": "USA"
};

for (let code in codes) {
  alert(code); // 1, 41, 44, 49
}

// to solve it : 
let codes = {
  "+49": "Germany",
  "+41": "Switzerland",
  "+44": "Great Britain",
  // ..,
  "+1": "USA"
};

for (let code in codes) {
  alert( +code ); // 49, 41, 44, 1
}