Deep copy of objects

Polyfill for bind also

by Anchit Gupta

JavaScript

let obj = {
	id : 1,
	details : {
		name: "Anchit",
		age : 26,
		printName : function(){
			console.log("my name is: ", this.name);
		} 
	},
	other : {
		random : {
			type: "text"
		}
	}
};

const deepCopyFunc = (inObj) =>{
	let outObj, key, value;
  if (typeof inObj === null || typeof inObj !== "object"){
  	return inObj;
  }
  
  outObj = Array.isArray(inObj) ? [] : {};
  
  for (key in inObj){
  	value = inObj[key];
  	outObj[key] = deepCopyFunc(value);
  }
  return outObj;
}

let obj2 = deepCopyFunc(obj);

console.log(obj2);

// polyfill for bind

Function.prototype.mybind = function(...args){
	let context = this,
  		params 	= args.slice(1); 
  return function(...args2){
  	context.apply(args[0], [...params, ...args2]);
  }
}