Skip to content

Instantly share code, notes, and snippets.

@JamieMason
Created December 16, 2010 12:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamieMason/743330 to your computer and use it in GitHub Desktop.
Save JamieMason/743330 to your computer and use it in GitHub Desktop.
The examples of how to share protected data within an inheritance chain in JavaScript using Douglas Crockford's Functional Inheritance Pattern I didn't find particularily clear. I use this example below to remind myself how I perceive it works.
var oSuperclass = function (spec, oProtected)
{
oProtected = oProtected || {};
var that = {};
oProtected.supMethod = function()
{
console.log('superclass says: ', spec.jamie);
};
return that;
}
var oSubclass = function (spec)
{
var oProtected = {},
that = oSuperclass(spec, oProtected);
oProtected.subMethod = function()
{
console.log('subclass says: ', spec.jamie);
};
oProtected.supMethod();
oProtected.subMethod();
return that;
}
var instance = oSubclass({
jamie : "mason"
});
>>> superclass says: mason
>>> subclass says: mason
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment