473,326 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

inherited private variables

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.
Jun 27 '08 #1
13 1697
PragueExpat wrote:
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;
}
[snip]

a and b are not private variables here. They are members of the object
so the are public to anyone with access to that object.

As a result, the rest of your pattern doesn't really show anything as
you don't have any private members in your code at all (except, perhaps
'_', but that is private in your final object, so accessing it is not a
big achievement.
Jun 27 '08 #2
On Jun 9, 9:12 pm, Dan Rumney <danrum...@warpmail.netwrote:
PragueExpat wrote:
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;
}

[snip]

a and b are not private variables here. They are members of the object
so the are public to anyone with access to that object.

As a result, the rest of your pattern doesn't really show anything as
you don't have any private members in your code at all (except, perhaps
'_', but that is private in your final object, so accessing it is not a
big achievement.

I'm not so sure. The constructors priv and priv2 are not used to
create any public objects. The are used to create exactly one object
( _ ), which is private to the object 'test'. If you can see a way to
access a, b, c or d from outside of a privileged method in the
function ob, please show me how.
Jun 27 '08 #3
PragueExpat <ri********@gmail.comwrites:
I'm not so sure. The constructors priv and priv2 are not used to
create any public objects. The are used to create exactly one object
( _ ), which is private to the object 'test'. If you can see a way to
access a, b, c or d from outside of a privileged method in the
function ob, please show me how.
priv2.prototype.a

priv2.prototype.b

Ofcourse, you *could* delete priv2.prototype after instantiating all
the priv2 objects you need.

The only sane way to get really private properties in javascript is to
not use properties at all and use closures instead. But that's hardly
news.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Jun 27 '08 #4
PragueExpat wrote:
[snip]
>>
a and b are not private variables here. They are members of the object
so the are public to anyone with access to that object.

As a result, the rest of your pattern doesn't really show anything as
you don't have any private members in your code at all (except, perhaps
'_', but that is private in your final object, so accessing it is not a
big achievement.


I'm not so sure. The constructors priv and priv2 are not used to
create any public objects. The are used to create exactly one object
( _ ), which is private to the object 'test'. If you can see a way to
access a, b, c or d from outside of a privileged method in the
function ob, please show me how.
My point was that you didn't inherit any private functions.

You created an object with a private member which was a priv2 object.
This object had publicly available members a,b,c and d.

However, I now understand what you're getting at.

Take a look at:
http://crockford.com/javascript/private.html

It talks about accessing an object's private members from a public method.
Jun 27 '08 #5
On Jun 9, 9:35 pm, Joost Diepenmaat <jo...@zeekat.nlwrote:
PragueExpat <richhen...@gmail.comwrites:
I'm not so sure. The constructors priv and priv2 are not used to
create any public objects. The are used to create exactly one object
( _ ), which is private to the object 'test'. If you can see a way to
access a, b, c or d from outside of a privileged method in the
function ob, please show me how.

priv2.prototype.a

priv2.prototype.b

Ofcourse, you *could* delete priv2.prototype after instantiating all
the priv2 objects you need.

The only sane way to get really private properties in javascript is to
not use properties at all and use closures instead. But that's hardly
news.

--
Joost Diepenmaat | blog:http://joost.zeekat.nl/| work:http://zeekat.nl/
Yes, you can call the .prototype (not IE, of course) but that only
gives the original value - my goal is to share and update private
information among inherited 'classes'. If a method in priv changed
property 'a' to 2, then there is no way to access that value, because
closures are being used.

Because the private variable ( _ ) is an object, you can change its
properties via private or privileged methods and they cannot be
accessed.

You could also use private functions in the constructor ob like this:

function ob(){
var _ = new priv2();
var times = function(){_.c * 2);
return {
go:function(){
alert(_.a);
}
}

}
Thoughts?
Jun 27 '08 #6
On Jun 9, 10:09 pm, Dan Rumney <danrum...@warpmail.netwrote:
PragueExpat wrote:

[snip]
a and b are not private variables here. They are members of the object
so the are public to anyone with access to that object.
As a result, the rest of your pattern doesn't really show anything as
you don't have any private members in your code at all (except, perhaps
'_', but that is private in your final object, so accessing it is not a
big achievement.
I'm not so sure. The constructors priv and priv2 are not used to
create any public objects. The are used to create exactly one object
( _ ), which is private to the object 'test'. If you can see a way to
access a, b, c or d from outside of a privileged method in the
function ob, please show me how.

My point was that you didn't inherit any private functions.

You created an object with a private member which was a priv2 object.
This object had publicly available members a,b,c and d.

However, I now understand what you're getting at.

Take a look at:http://crockford.com/javascript/private.html

It talks about accessing an object's private members from a public method.
Yes, you are correct that I didn't inherit private function - I
probably should have titled this thread something else. I'm just
really curious to see if this is a way to inherit properties/methods
that cannot be accessed from outside privileged methods. The reason
for this idea was because it is difficult to inherit objects that have
private functions and I was looking for a way to do that.

I'm still not sure that its viable but I'm not yet convinced that its
not.
Jun 27 '08 #7
PragueExpat <ri********@gmail.comwrites:
Yes, you can call the .prototype (not IE, of course)
You mean "also in IE, of course". I hope. This is fully supported and
standardized behaviour.
but that only gives the original value - my goal is to share and
update private information among inherited 'classes'. If a method in
priv changed property 'a' to 2, then there is no way to access that
value, because closures are being used.
That's because if you set the property, you're not inheriting it
anymore.

See section "property lookup" in
http://joost.zeekat.nl/constructors-...confusing.html
Because the private variable ( _ ) is an object, you can change its
properties via private or privileged methods and they cannot be
accessed.
Yes, that's because it's a local variable, captured by a closure, and
you *can* create "truely private" "properties" using closures. But
none of that has anything to do with inheritance, or properties,
really. Just because you're closing over an object that uses
inheritance doesn't mean the inheritance chain and its properties are
now somehow private. It just means you're making that one object
private.
You could also use private functions in the constructor ob like this:

function ob(){
var _ = new priv2();
var times = function(){_.c * 2);
return {
go:function(){
alert(_.a);
}
}

}
I know. :-)
Thoughts?
I still don't see how inheritance comes in to this. Maybe my mind is
slow today, but I think you're ascribing more functionality to the
code than is actually shown.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Jun 27 '08 #8
First of all, thanks for the feedback - very interesting

On Jun 9, 10:25 pm, Joost Diepenmaat <jo...@zeekat.nlwrote:
PragueExpat <richhen...@gmail.comwrites:
Yes, you can call the .prototype (not IE, of course)

You mean "also in IE, of course". I hope. This is fully supported and
standardized behaviour.
you can call object.prototype in IE? I'm fairly sure that IE doesn't
support that (I'm not talking about setting a function's prototype,
but rather getting the prototype of an existing object)

function a(){

}
a.prototype={
b:1
}

calling a.prototype in IE will come back with undefined
but that only gives the original value - my goal is to share and
update private information among inherited 'classes'. If a method in
priv changed property 'a' to 2, then there is no way to access that
value, because closures are being used.

That's because if you set the property, you're not inheriting it
anymore.
Yes, I see what you're saying -I'm not trying so much to inherit
static properties, but also private functions. And by private
functions, maybe I'm using that in the wrong sense - I mean functions
that cannot be accessed on an object except through privileged
methods.
>
See section "property lookup" inhttp://joost.zeekat.nl/constructors-considered-mildly-confusing.html
Because the private variable ( _ ) is an object, you can change its
properties via private or privileged methods and they cannot be
accessed.

Yes, that's because it's a local variable, captured by a closure, and
you *can* create "truely private" "properties" using closures. But
none of that has anything to do with inheritance, or properties,
really. Just because you're closing over an object that uses
inheritance doesn't mean the inheritance chain and its properties are
now somehow private. It just means you're making that one object
private.
OK, that makes sense.
>
You could also use private functions in the constructor ob like this:
function ob(){
var _ = new priv2();
var times = function(){_.c * 2);
return {
go:function(){
alert(_.a);
}
}
}

I know. :-)
Thoughts?

I still don't see how inheritance comes in to this. Maybe my mind is
slow today, but I think you're ascribing more functionality to the
code than is actually shown.
I thought of this pattern because normally I do this:

var x = function(){

//private
var a = function...
var b = function...

return {
c:function...
d:function...
}

and reusing the private functions in an inherited object is not
possible. I was trying to find a way that an inherited object could
also get access to the private functions for use.
}
--
Joost Diepenmaat | blog:http://joost.zeekat.nl/| work:http://zeekat.nl/
Jun 27 '08 #9
PragueExpat <ri********@gmail.comwrites:
First of all, thanks for the feedback - very interesting

On Jun 9, 10:25 pm, Joost Diepenmaat <jo...@zeekat.nlwrote:
>PragueExpat <richhen...@gmail.comwrites:
Yes, you can call the .prototype (not IE, of course)

You mean "also in IE, of course". I hope. This is fully supported and
standardized behaviour.

you can call object.prototype in IE? I'm fairly sure that IE doesn't
support that (I'm not talking about setting a function's prototype,
but rather getting the prototype of an existing object)
*I was* talking about getting a function's prototype property. Not an
object's "hidden" prototype.
function a(){

}
a.prototype={
b:1
}

calling a.prototype in IE will come back with undefined
No it won't. You really should test that.

< snip >
>I still don't see how inheritance comes in to this. Maybe my mind is
slow today, but I think you're ascribing more functionality to the
code than is actually shown.

I thought of this pattern because normally I do this:

var x = function(){

//private
var a = function...
var b = function...

return {
c:function...
d:function...
}

and reusing the private functions in an inherited object is not
possible. I was trying to find a way that an inherited object could
also get access to the private functions for use.
The only way to do that, that I can see, is to define the relevant
methods of the inheriting object in the same scope as the var a etc
functions here - in other words, close over the the functions in the
"sub" object. Any other way will just move the problem around (or make
the properties "public" again).

Closures are orthogonal to the whole object mechanism in javascript,
which is why they provide such good isolation, but that also means you
can't really get (at) private variables via the standard
inheritance/prototyping mechanism. (but it's possible to build objects
and inheritance out of closures and work the other way around, if you
think javascript isn't slow and verbose enough).

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Jun 27 '08 #10
On Jun 9, 11:14 pm, Joost Diepenmaat <jo...@zeekat.nlwrote:
PragueExpat <richhen...@gmail.comwrites:
First of all, thanks for the feedback - very interesting
On Jun 9, 10:25 pm, Joost Diepenmaat <jo...@zeekat.nlwrote:
PragueExpat <richhen...@gmail.comwrites:
Yes, you can call the .prototype (not IE, of course)
You mean "also in IE, of course". I hope. This is fully supported and
standardized behaviour.
you can call object.prototype in IE? I'm fairly sure that IE doesn't
support that (I'm not talking about setting a function's prototype,
but rather getting the prototype of an existing object)

*I was* talking about getting a function's prototype property. Not an
object's "hidden" prototype.
function a(){
}
a.prototype={
b:1
}
calling a.prototype in IE will come back with undefined

No it won't. You really should test that.
You are right - I stand corrected :)
>
< snip >
I still don't see how inheritance comes in to this. Maybe my mind is
slow today, but I think you're ascribing more functionality to the
code than is actually shown.
I thought of this pattern because normally I do this:
var x = function(){
//private
var a = function...
var b = function...
return {
c:function...
d:function...
}
and reusing the private functions in an inherited object is not
possible. I was trying to find a way that an inherited object could
also get access to the private functions for use.

The only way to do that, that I can see, is to define the relevant
methods of the inheriting object in the same scope as the var a etc
functions here - in other words, close over the the functions in the
"sub" object. Any other way will just move the problem around (or make
the properties "public" again).

Closures are orthogonal to the whole object mechanism in javascript,
which is why they provide such good isolation, but that also means you
can't really get (at) private variables via the standard
inheritance/prototyping mechanism. (but it's possible to build objects
and inheritance out of closures and work the other way around, if you
think javascript isn't slow and verbose enough).

--
Joost Diepenmaat | blog:http://joost.zeekat.nl/| work:http://zeekat.nl/
Even if the constructors priv and priv2 have public members, the
resulting object is assigned to a private variable so in effect, the
result is what I was looking for: an object of variables/methods that
cannot be accessed except for the privileged methods. And I can reuse
or extend that object in the private scope of another object.

and if my constructor ob looked like this:

function ob(){
var _ = new priv2();
return {
init:function(){
_.that = this;
}
go:function(){
alert(_.a);
}
}

}

the private variable _ would have a pointer to the public object
properties

Don't you think that there is some value in that?
Jun 27 '08 #11
PragueExpat <ri********@gmail.comwrites:
Even if the constructors priv and priv2 have public members, the
resulting object is assigned to a private variable so in effect, the
result is what I was looking for: an object of variables/methods that
cannot be accessed except for the privileged methods. And I can reuse
or extend that object in the private scope of another object.

and if my constructor ob looked like this:

function ob(){
var _ = new priv2();
return {
init:function(){
_.that = this;
}
go:function(){
alert(_.a);
}
}

}

the private variable _ would have a pointer to the public object
properties

Don't you think that there is some value in that?
To be honest, I can see only very limited real uses for this kind of
construct, as far as it relates to inheritance, OO programming and
"protection" in javascript.

I like closures and functional programming just fine, but especially
in JS, where methods are mutable properties, I don't really see the
point of using closures to emulate private variables - you still need
a mutable method to get at them. AFAICT they're probably much more
useful to emulate class variables. IOW, I think closures definitely
have their place, but just using them for access control is missing
several points:

1. Closures are a lot more useful than that.

2. You don't want to control access, you just want to prevent name
collisions for truely private variables, so you can use _ or i or x
without overwriting some other function's _, i or x. Sane languages
already do this when you use (lexical or even dynamic) variables.

2. Object property/method access control is overrated:

*in general... (not aimed at you or anyone else in particular):*

As far as I can see, the public/private/protected attributes of
properties in most languages and most systems are just documentation
markers. They shouldn't be taken (or written) as infallible
protection; they just indicate the currently projected use of an API;
if you can't switch some property or method from private to public
when needed, you should probably make it public from the start (mark
it as private or unsupported in the documentation, choose a better
property/method name while you're at it).

And whoever thought up "protected" had better be damn sorry.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Jun 27 '08 #12
On Jun 10, 12:13 am, Joost Diepenmaat <jo...@zeekat.nlwrote:
PragueExpat <richhen...@gmail.comwrites:
Even if the constructors priv and priv2 have public members, the
resulting object is assigned to a private variable so in effect, the
result is what I was looking for: an object of variables/methods that
cannot be accessed except for the privileged methods. And I can reuse
or extend that object in the private scope of another object.
and if my constructor ob looked like this:
function ob(){
var _ = new priv2();
return {
init:function(){
_.that = this;
}
go:function(){
alert(_.a);
}
}
}
the private variable _ would have a pointer to the public object
properties
Don't you think that there is some value in that?

To be honest, I can see only very limited real uses for this kind of
construct, as far as it relates to inheritance, OO programming and
"protection" in javascript.

I like closures and functional programming just fine, but especially
in JS, where methods are mutable properties, I don't really see the
point of using closures to emulate private variables - you still need
a mutable method to get at them. AFAICT they're probably much more
useful to emulate class variables. IOW, I think closures definitely
have their place, but just using them for access control is missing
several points:

1. Closures are a lot more useful than that.

2. You don't want to control access, you just want to prevent name
collisions for truely private variables, so you can use _ or i or x
without overwriting some other function's _, i or x. Sane languages
already do this when you use (lexical or even dynamic) variables.

2. Object property/method access control is overrated:

*in general... (not aimed at you or anyone else in particular):*

As far as I can see, the public/private/protected attributes of
properties in most languages and most systems are just documentation
markers. They shouldn't be taken (or written) as infallible
protection; they just indicate the currently projected use of an API;
if you can't switch some property or method from private to public
when needed, you should probably make it public from the start (mark
it as private or unsupported in the documentation, choose a better
property/method name while you're at it).

And whoever thought up "protected" had better be damn sorry.

--
Joost Diepenmaat | blog:http://joost.zeekat.nl/| work:http://zeekat.nl/
Thanks again, Joost, for your insight. I appreciate the time you took
to talk about this.
Jun 27 '08 #13
*in general... (not aimed at you or anyone else in particular):*
>
As far as I can see, the public/private/protected attributes of
properties in most languages and most systems are just documentation
markers. They shouldn't be taken (or written) as infallible
protection; they just indicate the currently projected use of an API;
if you can't switch some property or method from private to public
when needed, you should probably make it public from the start (mark
it as private or unsupported in the documentation, choose a better
property/method name while you're at it).

And whoever thought up "protected" had better be damn sorry.
Totally agree, public, private and protected definitions should be
used for organizing the code not for "securing" it... at least for
most languages. In JS, on the other hand I can imagine a couple of
situations where it could be used for a bit of "security".
Jun 27 '08 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dave | last post by:
Hello NG, Regarding access-declarations and member using-declarations as used to change the access level of an inherited base member... Two things need to be considered when determining an...
4
by: Bryan Green | last post by:
So I'm working on a project for a C# class I'm taking, where I need to keep some running totals via static variables. I need three classes for three different types of objects. The base class and...
1
by: leal ting | last post by:
a class inherited from ArrayList, is saved to ViewState, why the type of the object read from ViewSate is not the class, but the parent, ArrayList lealting@hotmail.com] the class inherited...
2
by: twawsico | last post by:
I ran into this while converting some DX C# code to VB.NET (VS 2003), and I'm just curious as to whether this is intended behavior (and if so, where might I read up on it) or more of a bug. This...
6
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that...
2
by: GoCoogs | last post by:
I'm trying to count how many items are in a dynamic collection. This is the code I have so far. *** Begin Code *** Public Class Rule Private _rulevars As RuleVarsCollection Private _rulename...
0
by: VorTechS | last post by:
I'm having a problem with an inherited label, applying text rotation to aligned text. If text rotation is applied to the aligned text, the alignment goes 'nuts'. I can find no logic to what is...
1
by: David Belohrad | last post by:
Dear All, could someone give a hint? I'd like to share the resources as follows: A shared class which works as counter of number of shared resources: class Shared { public: Q_PCBShared()...
16
by: gwoodhouse | last post by:
Good Morning, Ive been programming in c# for a few months now, and one of the things i havnt quite figured out is this: I would like to have inherited classes with their own set of variables....
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.