At first, you don’t lose the prototyping effect when using the functional inheritance pattern. I just assume that you are talking about the good parts, crockford also introduced a fairly simple and efficient way to have common variables for this template. What basically looks like:
var Human = (function(my, priv) { var my = my || {}, priv = priv || {}; my.privatedata = "foobar"; priv.walk = function() { console.log('walking'); return priv; }; priv.talk = function() { console.log('blah blah'); return priv; }; return priv; }()); var Andy = (function(my, priv) { var my = my || {}, priv = Human(my, priv); priv.SpecialAndyThing = function() { console.log('bloggin at typeofnan.com'); return priv; }; return priv; }()); var myAndy = Andy(); myAndy.talk().SpecialAndyThing();
You can even extend this technique to have some super methods. Using critical convention variables, such as underlining or something, is not a bad practice at all. It is confusing because no one knows what is going on there (perhaps this argument fails if you are the only one using the codebase).
However, ECMAscript Edition 5 introduces some goodys to have more "private" members in the prototype chain. . One of the important methods for this is .defineProperty , where you can define a property that is not “shallow”. It will look like this:
var Human = {}; Object.defineProperty(Human, 'privateStuff', { value: 'secret', enumerable: false });
Now, the privateStuff property privateStuff not displayed for an object that inherits the Human prototype chain. In any case, this material requires Javascript 1.8.5 and is only available in modern browsers. See https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/defineProperty
jAndy source share