Add dynamic property in JavaScript function

  javascript

By Google, I’m getting how to add dynamic javascript property in an object but here my goal is a little bit different.

I have tried with this code but it gives me undefined.

var infoObj = {
  first: "John",
  last: "Doe"
};


function DD(){
  
  var me = this;
  
  Object.keys(infoObj).forEach(function(key) {
        me[key] = infoObj[key]
  });
}

console.log(DD.first)
//undefined

Source: Ask Javascript Questions

LEAVE A COMMENT