object-mixin
ES6 next big thing polyfilled before it gets real
Last updated 14 years ago by webreflection .
Repository · Original npm · Tarball · package.json
$ npm install object-mixin 
SYNC missed versions from official npm registry.

object-mixin

A possible ES6 polyfill

build status

API


// basic signature
Object.mixin(
  target:Object,  // the object to be enriched

  source:Object   // all own properties of this object
                  // will be assigned using same descriptor

):target          // returns the target object modified


// overload with Traits (Function)

Object.mixin(
  target:Object,  // the object to be enriched

  source:Trait    // invoked with target as context

  [, args:Array|Arguments]
                  // optional args property
                  // used at Trait arguments

):target          // returns the target object modified

Compatibility

Should be compatible with all JavaScript ES3 or JScript engines.

node.js module, AMD module, and generic env standalone file.

Example

// using an object

var a = {a: 'b'},
    b = Object.mixin({b: 'b'}, a);

console.log(JSON.stringify(b));
// {"a":"a","b":"b"}


// using a function
function addUniqueIdFunctionality(prefix) {
  var
    privatelyShared = {},
    i = 0
  ;
  if (!prefix) prefix = '.' + (new Date).getTime();
  this.getIdFor = function (key) {
    return privatelyShared[key] || (
      privatelyShared[key] = ++i + prefix + Math.random()
    );
  };
}

function GenericClass(){}

Object.mixin(
  GenericClass.prototype,
  addUniqueIdFunctionality
  // , ['my_prefix']
);

var instance1 = new GenericClass,
    instance2 = new GenericClass;

console.log([
  instance1.getIdFor('a'),
  instance1.getIdFor('b'), // different
  instance2.getIdFor('a')
  // same as instance1.getIdFor('a')
].join('\n'));

License

           DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
                   Version 2, December 2004

Copyright (C) 2013 Andrea Giammarchi <spam@hater.me>

Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.

           DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

 0. You just DO WHAT THE FUCK YOU WANT TO.

Current Tags

  • 0.2.1                                ...           latest (14 years ago)

3 Versions

  • 0.2.1                                ...           14 years ago
  • 0.1.1                                ...           14 years ago
  • 0.1.0                                ...           14 years ago
Maintainers (1)
Downloads
Total 13
Today 0
This Week 0
This Month 4
Last Day 0
Last Week 1
Last Month 3
Dependencies (1)
Dev Dependencies (0)
None
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE