Skip to main content
added 1326 characters in body
Source Link
user432683
user432683
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.

var lined = (function() {
    
    var liner = () => { return { fromX: 0, fromY: 0, toX: 0, toY: 0 }; };
    
    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.

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.

Source Link
user432683
user432683

Build an object going around its constructor. It seems confusing, a favourable chance for an imagination exercise omitting the theory just by its name, prototype design pattern, it's close to templating in that it centralises the description of an object; to get a sense of a proper use of it consider the used programming language. With strongly typed programming languages might be less obvious that it is different with loosely typed programming languages being possible to create objects without previously defining a type.

With JavaScript among others, objects can be created using object literals...

var line = { fromX: 0, fromY: 0, toX: 0, toY: 0 };
var anotherLine = { fromX: 0, fromY: 0, toX: 0, toY: 0 };
var otherLine = { fromX: 0, fromY: 0, toX: 0, toY: 0 };

...that with prototype pattern may be defined in a single place...

var liner = () => {
    return { fromX: 0, fromY: 0, toX: 0, toY: 0 };
};

...and then it can be used calling the function returning it:

var lined = (function() {
    
    var liner = () => { return { fromX: 0, fromY: 0, toX: 0, toY: 0 }; };
    
    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.