Skip to content

Instantly share code, notes, and snippets.

@tauren
Last active December 31, 2015 16:39

Revisions

  1. tauren revised this gist Dec 18, 2013. 1 changed file with 19 additions and 19 deletions.
    38 changes: 19 additions & 19 deletions constructorAOP.js
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@

    // Sipmle AOP after function
    var after = function(base, after) {
    if (!base) {
    return after;
    }
    if (!base) {
    return after;
    }
    return function composedAfter() {
    var res = base.apply(this, arguments);
    after.apply(this, arguments);
    @@ -13,24 +13,24 @@ var after = function(base, after) {

    // Functional mixin to add the talk feature to an object
    var withTalk = function(destination) {
    destination.talkConstructor = function() {
    console.log('Constructing talker '+this.name);
    };
    destination.talk = function() {
    console.log('My name is '+this.name);
    };
    destination.init = after(destination.init, function() {
    console.log('Initializing talker '+this.name);
    });
    console.log('Talk feature added to destination');
    destination.talkConstructor = function() {
    console.log('Constructing talker '+this.name);
    };
    destination.talk = function() {
    console.log('My name is '+this.name);
    };
    destination.init = after(destination.init, function() {
    console.log('Initializing talker '+this.name);
    });
    console.log('Talk feature added to destination');
    };

    console.log('\n-----------FOO----------');

    // Augmentation of init function
    function Foo (name) {
    console.log('Constructing foo '+name);
    this.name = name;
    console.log('Constructing foo '+name);
    this.name = name;
    }
    Foo.prototype.init = function() {
    console.log('Initializing foo '+this.name);
    @@ -47,10 +47,10 @@ console.log('\n-----------BAR----------');
    // Augmentation of constructor
    // PROBLEM: Requires talkConstructor to be called from constructor
    function Bar (name) {
    console.log('Constructing bar '+name);
    this.name = name;
    // OBJECTIVE: How can this call be removed?
    this.talkConstructor();
    console.log('Constructing bar '+name);
    this.name = name;
    // OBJECTIVE: How can this call be removed?
    this.talkConstructor();
    }
    Bar.prototype.init = function() {
    console.log('Initializing bar '+this.name);
  2. tauren created this gist Dec 18, 2013.
    85 changes: 85 additions & 0 deletions constructorAOP.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,85 @@

    // Sipmle AOP after function
    var after = function(base, after) {
    if (!base) {
    return after;
    }
    return function composedAfter() {
    var res = base.apply(this, arguments);
    after.apply(this, arguments);
    return res;
    };
    };

    // Functional mixin to add the talk feature to an object
    var withTalk = function(destination) {
    destination.talkConstructor = function() {
    console.log('Constructing talker '+this.name);
    };
    destination.talk = function() {
    console.log('My name is '+this.name);
    };
    destination.init = after(destination.init, function() {
    console.log('Initializing talker '+this.name);
    });
    console.log('Talk feature added to destination');
    };

    console.log('\n-----------FOO----------');

    // Augmentation of init function
    function Foo (name) {
    console.log('Constructing foo '+name);
    this.name = name;
    }
    Foo.prototype.init = function() {
    console.log('Initializing foo '+this.name);
    };
    // Mix the Talk feature into Foo
    withTalk(Foo.prototype);

    var foo = new Foo('Timon');
    foo.init();
    foo.talk();

    console.log('\n-----------BAR----------');

    // Augmentation of constructor
    // PROBLEM: Requires talkConstructor to be called from constructor
    function Bar (name) {
    console.log('Constructing bar '+name);
    this.name = name;
    // OBJECTIVE: How can this call be removed?
    this.talkConstructor();
    }
    Bar.prototype.init = function() {
    console.log('Initializing bar '+this.name);
    };
    // Mix the Talk feature into Bar
    withTalk(Bar.prototype);

    var bar = new Bar('Pumba');
    bar.init();
    bar.talk();

    /*
    The following output is generated:
    -----------FOO----------
    Talk feature added to destination
    Constructing foo Timon
    Initializing foo Timon
    Initializing talker Timon
    My name is Timon
    -----------BAR----------
    Talk feature added to destination
    Constructing bar Pumba
    Constructing talker Pumba
    Initializing bar Pumba
    Initializing talker Pumba
    My name is Pumba
    */