Skip to content

Instantly share code, notes, and snippets.

@partkyle
Created November 23, 2012 23:57
Show Gist options
  • Select an option

  • Save partkyle/4137758 to your computer and use it in GitHub Desktop.

Select an option

Save partkyle/4137758 to your computer and use it in GitHub Desktop.

Revisions

  1. partkyle revised this gist Nov 24, 2012. 1 changed file with 52 additions and 0 deletions.
    52 changes: 52 additions & 0 deletions delegates.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    var Something, Messager;

    Something = (function() {

    function Something() {
    this.delegateObject = new Messager;
    }

    return Something;

    })();

    Messager = (function() {

    function Messager() {}

    Messager.prototype.testing = function(int) {
    return console.log(int);
    };

    Messager.prototype.woot = function(bool) {
    return console.log(bool);
    };

    Messager.prototype.nothing = function(array) {
    return console.log(array);
    };

    return Messager;

    })();

    Object.prototype.delegates = function(methods, to) {
    var _this = this;
    return methods.forEach(function(method) {
    return _this.prototype[method] = function() {
    var args;
    args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
    return this[to][method].apply(this, args);
    };
    });
    };

    Something.delegates(['testing', 'woot', 'nothing', 'missing'], 'delegateObject');

    s = new Something;

    s.testing(1);

    s.woot(false);

    s.nothing([1, 2, 4]);
  2. partkyle revised this gist Nov 23, 2012. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion delegates.coffee
    Original file line number Diff line number Diff line change
    @@ -15,4 +15,10 @@ Object::delegates = (methods, to) ->
    this::[method] = (args...) ->
    @[to][method].apply(this, args)

    Something.delegates(['testing','woot','nothing','missing'], 'delegateObject')
    Something.delegates(['testing','woot','nothing','missing'], 'delegateObject')

    s = new Something

    s.testing(1)
    s.woot(false)
    s.nothing([1,2,4])
  3. partkyle created this gist Nov 23, 2012.
    18 changes: 18 additions & 0 deletions delegates.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    class Something
    constructor: ->
    @delegateObject = new Messager

    class Messager
    testing: (int) ->
    console.log int
    woot: (bool) ->
    console.log bool
    nothing: (array) ->
    console.log array

    Object::delegates = (methods, to) ->
    methods.forEach (method) =>
    this::[method] = (args...) ->
    @[to][method].apply(this, args)

    Something.delegates(['testing','woot','nothing','missing'], 'delegateObject')