Created
January 31, 2011 07:24
-
-
Save Nutrox/803735 to your computer and use it in GitHub Desktop.
JavaScript - Private/Static Property Example
This file contains hidden or 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 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 |
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
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.