P
PragueExpat
I (think) that I've come up with a pattern that I haven't seen in any
publications so far and I would like some feedback. Basically, I was
looking for a way to inherit private functions and I came up with
this:
//base private object constructor
function priv(){
this.a = 1;
this.b = 2;
}
//private object constructor that inherits from base private object
function priv2(){
this.c = 3;
this.d = 4;
}
priv2.prototype = new priv();
//constructor that uses private object in private namespace
function ob(){
var _ = new priv2();
return {
go:function(){
alert(_.a);
}
}
}
var test = new ob();
//returns 1
test.go();
//only go() is public
test;
-------------------------------------------
First of all, is this a known pattern? If so, sorry for the redundant
information. If not, are there any drawbacks that you can see? The
advantage, as I see it, is that inherited objects can also inherit
private functions and properties for use.
Any comments would be welcome.
publications so far and I would like some feedback. Basically, I was
looking for a way to inherit private functions and I came up with
this:
//base private object constructor
function priv(){
this.a = 1;
this.b = 2;
}
//private object constructor that inherits from base private object
function priv2(){
this.c = 3;
this.d = 4;
}
priv2.prototype = new priv();
//constructor that uses private object in private namespace
function ob(){
var _ = new priv2();
return {
go:function(){
alert(_.a);
}
}
}
var test = new ob();
//returns 1
test.go();
//only go() is public
test;
-------------------------------------------
First of all, is this a known pattern? If so, sorry for the redundant
information. If not, are there any drawbacks that you can see? The
advantage, as I see it, is that inherited objects can also inherit
private functions and properties for use.
Any comments would be welcome.