Last active
February 15, 2016 12:17
-
-
Save TomK/5cadf0377a8ba7634680 to your computer and use it in GitHub Desktop.
Javascript object method with working private and static
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
/** METHOD ONE **/ | |
/** DOES NOT WORK WITH instanceof **/ | |
var MyClass = (function () | |
{ | |
/* STATIC */ | |
// private static | |
var _privateStatic = 'static private initial'; | |
// public static | |
_class.staticProperty = 'static initial'; | |
/* NON-STATIC */ | |
function _class(initialPublic) | |
{ | |
var _privateProperty = 'private initial'; | |
function __construct() | |
{ | |
console.log('init with', initialPublic); | |
this.publicProperty = initialPublic; | |
} | |
__construct.prototype.getPrivate = function () | |
{ | |
return _privateProperty; | |
}; | |
__construct.prototype.setPrivate = function (value) | |
{ | |
_privateProperty = value; | |
}; | |
return new __construct(); | |
} | |
return _class; | |
})(); | |
var p1 = new MyClass('initial test 1'); | |
p1.setPrivate('test1'); | |
var p2 = new MyClass('initial test 2'); | |
p2.setPrivate('test2'); | |
console.log('static', MyClass.staticProperty); | |
MyClass.staticProperty = 'non-initial'; | |
console.log('p1', p1.getPrivate(), p1.publicProperty, MyClass.staticProperty, p1 instanceof MyClass); | |
console.log('p2', p2.getPrivate(), p2.publicProperty, MyClass.staticProperty, p2 instanceof MyClass); | |
/* | |
OUTPUT: | |
init with initial test 1 | |
init with initial test 2 | |
static static initial | |
p1 test1 initial test 1 non-initial false | |
p2 test2 initial test 2 non-initial false | |
*/ | |
/** METHOD TWO **/ | |
/** WORKS WITH instanceof, BUT PUBLIC FUNCTION CLOSURES CREATED FOR EACH INSTANCE **/ | |
var MyClass = (function () | |
{ | |
/* PRIVATE STATIC */ | |
var _privateStatic = 'static private initial'; | |
/* PUBLIC STATIC */ | |
_class.staticProperty = 'static initial'; | |
/* INSTANCE CONSTRUCTOR */ | |
function _class(initialPublic) | |
{ | |
/* PRIVATE */ | |
var _privateProperty = 'private initial'; | |
/* PUBLIC */ | |
this.getPrivate = function () | |
{ | |
return _privateProperty; | |
}; | |
this.setPrivate = function (value) | |
{ | |
_privateProperty = value; | |
}; | |
/* CONSTRUCTION CODE */ | |
console.log('init with', initialPublic); | |
this.publicProperty = initialPublic; | |
} | |
return _class; | |
})(); | |
var p1 = new MyClass('initial test 1'); | |
p1.setPrivate('test1'); | |
var p2 = new MyClass('initial test 2'); | |
p2.setPrivate('test2'); | |
console.log('static', MyClass.staticProperty); | |
MyClass.staticProperty = 'non-initial'; | |
console.log('p1', p1.getPrivate(), p1.publicProperty, MyClass.staticProperty, p1 instanceof MyClass); | |
console.log('p2', p2.getPrivate(), p2.publicProperty, MyClass.staticProperty, p2 instanceof MyClass); | |
/* | |
OUTPUT: | |
init with initial test 1 | |
init with initial test 2 | |
static static initial | |
p1 test1 initial test 1 non-initial true | |
p2 test2 initial test 2 non-initial true | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment