Currying with closures!
Prefill arguments and set
by Steven Senkus
HTML
<pre>Current Position: <br /></pre>
JavaScript
var setup = function (opts) {
this.opts = opts;
var currentPosition = opts.center;
if (opts.write === true) {
$('pre').append(JSON.stringify(currentPosition) + '<br />')
}
return {
move: function (units) {
currentPosition.x += opts.gridSize * units.x;
currentPosition.y += opts.gridSize * units.y;
return this;
},
write: function () {
$('pre').append(JSON.stringify(currentPosition) + '<br />')
return this;
}
};
};
var myPath = setup({
gridSize: 10,
center: {
x: 10,
y: 10
},
end: {
x: 200,
y: 200
},
write: true
});
myPath.move({
x: 0,
y: 5
}).write();
myPath.move({
x: 5,
y: -8
}).write();