Basic Example

Demo

In the example we see that we can easily override the properties of the instances by passing in an options object.

Code

Let's take an example of where this might be useful. Suppose we have some instance where we are defining some default configuration. However, we would like the user to be able to change the behaviour of the instance be passing in some options. Mutation allows us to setup a format for such configuration without explicitly needing to define such override behaviour.

Define our class
var Foo = function(options) {
    // Set the configuration of the class by
    // mutating some defaults with the options passed in
    this.configuration = Mutation.extendWith({
        hands: ['left', 'right'],
        evil: true,
        children: {
            'dave': {},
            'taylor': {}
        }
    }, options);
};
// Add a get method to retrieve a property
Foo.prototype.get = function(property) {
    return this.configuration[property];
};
// Create our instances
var instance1 = new Foo();
var instance2 = new Foo({
    'delete.evil': true,
    'insertAt[0].hands': 'center',
    'extend.children': {
         'nate': {},
         'pate': {},
         'chris': {}
     }
});

console.log(instance1.get('evil')); // true
console.log(instance2.get('evil')); // undefined

console.log(instance1.get('hands')); // ['left','right']
console.log(instance2.get('hands')); // ['center','left','right']

console.log(instance1.get('children')); // dave and taylor
console.log(instance2.get('children')); // dave, taylor, nate, pat and chris