var lined = (function() {
var liner = () => { return { fromX: 0, fromY: 0, toX: 0, toY: 0 }; };
var lined = (function() {
return (function() {
var originX = void(0);
var originY = void(0);
var toX = void(0);
var toY = void(0);
return { from : function(x, y) { originX = x; originY = y; return this; }
, to : function(x, y) { toX = x; toY = y; return this; }
, supply : function() {
var line = liner(); // get the prototype
line.fromX = originX;
line.fromY = originY;
line.toX = toX;
line.toY = toY;
return line;
}};
})();
})();
var line = lined.from(1, 1).to(10, 10).supply();
var anotherLine = lined.from(5, 5).to(10, 10).supply();
var otherLine = lined.from(20, 20).to(10, 10).supply();
Without line
definition from liner
function the object literal would have been spread all over the code turning it into a difficult to maintain one.
Prototype pattern is handy also to find out whether objects are or aren't of a certain type. With some boiler plate code...
var fields = (() => {
var source = {};
return { of: function(object) { source = object; return this; }
, to: { array: function() {
var fields = [];
for ( var field in source ) {
fields.push(field);
}
return fields;
}}};
})();
var is = function(object) {
return { type : { of : function(type) {
var guidee = fields.of(object).to.array();
var guide = fields.of(type).to.array();
var congruous = guide.length == guidee.length;
for ( var field in guide ) {
congruous = congruous && field in guidee;
}
return congruous;
}}};};
...line
type congruousness could be found in a blink of an eye...
var guidline = liner();
var lineType = is(line).type.of(guideline);
var anotherType = is(anotherLine).type.of(guideline);
var otherType = is(otherLine).type.of(guideline);
... all last three variables being true.
This is what I imagine the prototype design pattern is about. Pardon the rambling.