Javascript Object methods every developer should know
let obj = {}; obj.name = “messi”; obj.year= 2018; obj.speak = function(){ return “My Name is “+this.name+” and this is year “+this.year; }Deep Copy
If you just need to copy only properties which are not functions — there is an efficient method. We are moving away from Object constructor here and using another global Object in JS — JSON
let deepCopyObj = JSON.parse(JSON.stringify(obj)); console.log(deepCopyObj);//{ name: 'messi', year: 2019 }Object.create()
You can also create object with Object.create() function this has additional flexibility that you can choose what will be prototype of your new object.
let createObj = Object.create(obj); console.log(createObj); //{} createObj.name = “Pk”; console.log(createObj.speak());// My Name is Pk and this is year 2019