Skip to content

Instantly share code, notes, and snippets.

@Nutrox
Created January 31, 2011 07:24
Show Gist options
  • Save Nutrox/803735 to your computer and use it in GitHub Desktop.
Save Nutrox/803735 to your computer and use it in GitHub Desktop.
JavaScript - Private/Static Property Example
function Class() {
}
Class.prototype = new function() {
var value = 100;
function getValue() {
return value++;
}
this.getValue = getValue;
}
var a = new Class();
var b = new Class();
var c = new Class();
console.log( a.getValue() ); // 100
console.log( b.getValue() ); // 101
console.log( c.getValue() ); // 102
@tekool
Copy link

tekool commented Jan 31, 2011

The problem here is the same as the one described on this blog post http://blog.stroep.nl/2011/01/javascript-for-as3-developers/#comment-53281 I can't see how to manage inheritance the subclass would have to overwrite prototype to declare its own methods.

@Nutrox
Copy link
Author

Nutrox commented Jan 31, 2011

I agree that inheritance would be a problem in this situation, but it's just a little example. JavaScript prototype inheritance is a nasty thing to work with anyway, I avoid it whenever possible :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment