JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://cdn.crafty-modules.com/crafty-DEBUG.js"></script>
JavaScript
Crafty.init(256, 256);
Crafty.background('rgb(0,0,0)');
//A Crafty component, with a method implementing lazy function definition
//Based on: http://peter.michaux.ca/articles/lazy-function-definition-pattern
Crafty.c('FnAsObj',
{
foo: function () { //a method of the Crafty component
var t = new Date(); //a new date object
foo = function() { //we define foo to be a new function
return t; //we simply return the new date object
}
return foo(); //original foo returns the new foo function
}
});
//We can use our Crafty component like so:
var craftyObj = Crafty.e("2D, DOM, FnAsObj")
craftyObj.attr({x: 0, y: 0, w: 233, h: 275})
//These date objects will be the same:
console.log( craftyObj.foo() );
console.log( craftyObj.foo() );
console.log( craftyObj.foo() );
console.log( craftyObj.foo() );