Skip to content

Instantly share code, notes, and snippets.

@iaincarsberg
Created October 6, 2011 15:59
Show Gist options
  • Save iaincarsberg/1267781 to your computer and use it in GitHub Desktop.
Save iaincarsberg/1267781 to your computer and use it in GitHub Desktop.
RequireJS - Compose.js - Chaining Multiple Constructors
Animal - this {}
Animal - debug: [ muppet , bert ]
Hello, my name is bert and I am a muppet
returned: { species: 'muppet', name: 'bert' }
****************************************
Muppet - this {}
Muppet - debug: [ ernie ]
Animal - this {}
Animal - debug: [ muppet , ernie ]
Hello, my name is ernie and I am a muppet
returned: { species: 'muppet', name: 'ernie', whom: [Function] }
****************************************
Muppet - this {}
Muppet - debug: [ undefined ]
Animal - this {}
Animal - debug: [ muppet , undefined ]
Hello, my name is undefined and I am a muppet
returned: { species: 'muppet',
name: undefined,
whom: [Function] }
/*global window console*/
(function (require, undefined) {
require(
[
'./lib/compose/compose'
],
function (
Compose
) {
var Animal = Compose(
function (species, name) {
console.log('Animal - this', this);
console.log('Animal - debug: [', species, ',', name, ']');
this.species = species;
this.name = name;
},
{
whom: function () {
console.log(
'Hello, my name is ' + this.name +
' and I am a ' + this.species
);
return this;
}
}
);
var Muppet = Compose(
function (name) {
console.log('Muppet - this', this);
console.log('Muppet - debug: [', name, ']');
Compose.call(this, new Animal('muppet', name));
},
{
// Nothing new is needed
}
);
console.log(
'returned:', new Animal('muppet', 'bert').whom()
);
console.log('****************************************');
console.log(
'returned:', new Muppet('ernie').whom()
);
console.log('****************************************');
console.log(
'returned:', new Muppet().whom()
);
}
);
}(typeof window === 'undefined' ? require('./lib/r') : require));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment