JS Objects exploration (1)

JS Objects exploration (1)

Definition, creation and iterating over an object

Hello Guys ,

In this blog we'll explore JS objects

1 ) What is javascript objects:

An object is a collection of properties and a property is a relationship between a key and value pair .

2 ) How to create an object :

We can create object in different ways :

  • Using object literal :
const obj = {
 greet : 'hello'
}
  • Using Object.create() method
Object.create( { name: 'Dicko' } )
  • Using the class syntax :
class Person {
  name =  'Dicko'
  }
let person = new Person()
console.log( person.name) // Dicko
  • Using function as a constructor :
let Obj = function(){
  this.name = 'Dicko'
  return {
    property : this.name
  }
}
let name = new Obj()
console.log('name',name.property)  // Dicko

3 ) Manipulating by Value vs. Reference :

An object is a reference types that mean when you make copies of them, you're really just copying the references to that object in contrast to primitives values (Strings , Boolean ,Null ,Undefined and Numbers) are assigned or copied as a whole value . We'll better understand object copy in my next blog posting in a more details .

4 ) Looping through objects :

let object = {
    name: "Dicko",
    age: 27,
    gender: "Male"
};

// Iterating over object properties
for(var i in object) {  
    console.log(object[i] ); //  Dicko, 26 , Male
}

This a brief introduction in a core fundamental concept of JS , hope that it will be helpful for you

Thanks for reading, sharing and commenting if there is any errors let me know :) .

Source : Cover image : code.tutsplus.com/tutorials/inheritance-and..