I have a scenario where from a source object I need to create a new result object.
The object would need to have exactly all properties from source, with the addition of "methods", with naming based on the properties and code to be executed based on a template (in my case alert(this.x)).
Note: script should keep in consideration any number of properties from source
I would like to know:
- Is it possible to do it in JS?
- What the appropriate Technics?
- Any examples?
FROM SOURCE OBJECT
var source = {
a: 'a',
b: 'b'
};
I NEED TO GET RESULT OBJ DYNAMICALLY CREATED (BY SOME FUNCTION)
var result = {
a: 'a',
b: 'b',
_setA: function(){
alert(this.a);
},
_setB: function(){
alert(this.a);
}
}
Note: result is created after processing the source object
EDIT:
final solution based on your answers
sourcebe reflected when calling the methods onresult?