Created
December 16, 2010 12:13
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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