1

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

http://jsfiddle.net/kbnd6e5c/1/

3
  • just for clarification the object result should be created dynamically based on source properties Commented Dec 13, 2014 at 18:04
  • Should later mutations to source be reflected when calling the methods on result? Commented Dec 13, 2014 at 18:13
  • @sixfingeredman no reflection Commented Dec 13, 2014 at 18:16

3 Answers 3

1

You can first use $.extend method to copy properties, and then you need iterate through properties and create dynamic setters. For example like in below code:

var source = { a: 'a', b: 'b' };    
var result = $.extend({}, source);

Object.keys(source).forEach(function(key) {
    result['_set' + key[0].toUpperCase() + key.slice(1)] = function(value) {
        alert(this.key);
        this[key] = value;
    }
});

Check the working demo below.

var source = { a: 'a', b: 'b', test: 'test' };    
var result = $.extend({}, source);
    
Object.keys(source).forEach(function(key) {
    result['_set' + key[0].toUpperCase() + key.slice(1)] = function(value) {
        this[key] = value;
    }
});

alert([ result.a, result.test ]);

result._setA('a1');
result._setTest('test1');

alert([ result.a, result.test ]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, but I need the _setA and _setB created dynamically based on the properties in source
0

I'm not sure I have fully understanding your question but I think you should juste use that simple thing:

var copy = result
copy.example = {
    x: Y,
    z, T
}
copy.newFuntion = function(){...} //add your own function

This way you have everything that result have + your own things

1 Comment

copy is not, its a reference, appending to it will also append to the source
0

You can do something like this:

var result = {
    a: 'A',
    b: 'B'
};

var copy = result;

Object.keys(result).forEach(function(key) {
    Object.defineProperty(copy, "_set" + key, {
        get: function() {
            return result[key]; 
        }
    })
});

copy._seta;
copy._setb;

Source

1 Comment

hi, thanks, but it is not for getting or setting property is for creating dynamically method with some code to be executed inside.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.