js-wrap

Universal Wrapper Object System for ECMAScript 5 and beyond

wrap.js

Universal Wrapper Object System for ECMAScript 5 and beyond

<script src="wrap.js"></script>
require('wrap');
// for convenience 
var _ = Object.Wrap;
 
// singleton method! 
_(42)
  .learn('square', function() { return this*this })
  .square() * 1;  // 1764; 
(42).square();    // TypeError: Object 42 has no method 'square' 
 
// class method without changing Number 
_.Number.prototype.learn({
    timesfunction(f) { for (var i = 0; i < this; i++) f(i) },
    toThefunction(n) { return Math.pow(this, n) }
});
 // see your log! 
_(42).times(function(n){ console.log( _(2).toThe(n) ) });
 // TypeError: Object 42 has no method 'times' 
(42).times(function(n){ console.log(   (2).toThe(n) ) });

As seen above, Object.Wrap() wraps objects as transparently and painlessly as possible:

  • transparently
    • wrapped objects have same methods as its original
    • wrapped objects automatical unwraps on demand
  • painlessly
    • you can extend wrapped objects as easy as unwrapped version

So you can extend built-in methods without tainting built-in prototypes.

See wrap.md for details!