Created
April 12, 2012 19:22
-
-
Save mikechambers/2370306 to your computer and use it in GitHub Desktop.
Playing around ideas for prototype based inheritance that also provides private properties.
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
function Foo() | |
{ | |
var _a = "hello"; | |
if(!Foo.prototype.bar) | |
{ | |
Foo.prototype.bar = function() | |
{ | |
console.log("hi " + _a); | |
}; | |
Object.defineProperty( | |
Foo.prototype, | |
"a", | |
{ | |
get:function(){ | |
return _a; | |
}, | |
} | |
); | |
} | |
} | |
var f = new Foo(); | |
f.bar(); //hi hello | |
console.log(f._a); //undefined | |
console.log(f.a); //hello | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Actually, this doesnt work.
_a is essentially a private static variable shared between all instances of the class.
The only way I can think to do this, is to bind all of the when the class is instantiated. However, this means you can't use prototype based classes (each instance will have its own copy of functions / properties).