<h4>Decorator for Class</h4>
<div id="logger"></div>
<h4>How to create decorator for Module (by Richard Cornford)???</h4>
<div id="logger2"></div>
<h4>Or how to create private variables in Class</h4>
JavaScript
/* Class ****************************************/
function Base(params) {
this.v1 = params.v1;
this.v2 = params.v2;
}
Base.prototype = {
constructor: Base,
draw: function() { return ("v1: " + this.v1 + ", v2: " + this.v2); }
}
var b = new Base({v1: 3, v2: 5});
var logger = document.getElementById('logger');
logger.innerHTML = b.draw();
/* decorator pattern ********************************/
function BaseDecorated(base) {
this._base = base;
}
BaseDecorated.prototype = {
constructor: BaseDecorated,
draw: function() { return ("Decored --> " + this._base.draw()); }
}
var bDec = new BaseDecorated(new Base({v1: 34, v2: 56}));
logger.innerHTML = logger.innerHTML + "</br>" + bDec.draw();
/* Module by Richard Cornford ***************************/
function BaseMod(params) {
var _v1 = params.v1;
var _v2 = params.v2;
return {
getV1: function() { return _v1; },
getV2: function() { return _v2; },
setV1: function(v1) { _v1 = v1; }
}
}
var bm = BaseMod({v1: 13, v2: 98});
var logger2 = document.getElementById('logger2')
logger2.innerHTML = "v1: " + bm.getV1();
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.