473,495 Members | 2,058 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to access private variables directly from privilged function?

Hi there

Is there any way to access private variables directly from within a
priviliged function? I have a situation where the priviliged function's
execution context contains variables of the same name as the parent
context, but I want direct access to the parent context's variable.

E.g. I would like to be able to do this...

function Point()
{
var x = 0;
var y = 0;

this.setX(x)
{
// set private x with local x
}

this.setY(y)
{
// set private y with local y
}
}

I can obviously do this with

function Point()
{
var x = 0;
var y = 0;

this.setX(xLocal)
{
x = xLocal;
}

this.setY(yLocal)
{
y = yLocal;
}
}

But is there a way that avoids having to check for naming conflicts all
the time? I guess setX and setY will be assigned an execution context
and scope chain, but I need a way to access the scope chain one level
up, a bit like the "prototype" keyword can for "class" scope chains.
Problem is that "x" is being found in the first level so the next level
up is never checked. I'm no guru so I might be confusing things here,
but if I could do something *similar* to

this.setX(x)
{
prototype.x = x;
}

then I would be a very happy man... Cos in java I'm used to doing

void setX(int x)
{
this.x = x;
}

Thanks in advance!

May 26 '06 #1
2 3971
Rob Long wrote:
Is there any way to access private variables directly from within a
priviliged function? I have a situation where the priviliged function's
execution context contains variables of the same name as the parent
context, but I want direct access to the parent context's variable.
[...]
I can obviously do this with

function Point()
{
var x = 0;
var y = 0;

this.setX(xLocal)
{
x = xLocal;
}

this.setY(yLocal)
{
y = yLocal;
}
}

But is there a way that avoids having to check for naming conflicts all
the time? I guess setX and setY will be assigned an execution context
and scope chain, but I need a way to access the scope chain one level
up, a bit like the "prototype" keyword can for "class" scope chains.
The prototype object of a constructor is accessed through the prototype
chain, not through the scope chain. And there are no classes there, but
maybe you were already aware of that.
Problem is that "x" is being found in the first level so the next level
up is never checked. I'm no guru so I might be confusing things here,
but if I could do something *similar* to

this.setX(x)
{
prototype.x = x;
}

then I would be a very happy man... Cos in java I'm used to doing

void setX(int x)
{
this.x = x;
}


Since implementations of ECMAScript Ed. 1 to 3 have no built-in concept of
information hiding as Java has, and you cannot refer to the Variable Object
of an execution context (of which the variables declared for that context
are properties), it is not be possible to avoid those naming conflicts
other than

A) making sure the named argument has a name different than
one of the local variable of the superordered context,

B) not naming the arguments and using the `arguments' object instead

C) using (public) properties instead of local variables,

D) using an implementation of ECMAScript Edition 4 instead.

Unfortunately, there is no client-side production-level implementation of
ECMAScript Ed. 4 yet, most certainly because this edition is itself still
only a working draft proposed to the ECMA WG about four years ago.

So, I am afraid probably you will have to end up with something like

function Point(x, y)
{
this.setX(x);
this.setY(y);
}

Point.prototype = {
constructor: Point,

setX: function(x)
{
this.x = x || 0;
}

setY: function(y)
{
this.y = y || 0;
}
};

thereby sacrificing the "private" visibility property of the identifiers,
unless this is to work server-side (in which case you could use JScript.NET
and its implementation of ECMAScript Ed. 4 classes). Of course you could
also define setters and getters, but those are JavaScript 1.5+ only, so not
interoperable (IE, for example, supports JScript).
HTH

PointedEars
--
What one man can invent another can discover.
-- Sherlock Holmes in Sir Arthur Conan Doyle's
"The Adventure of the Dancing Men"
May 26 '06 #2
Thanks very much for your help, Thomas.

I had a feeling that would be the case. It's actually not too much of a
problem, more of a nice-to-have. I'm going to go with suggestions A and
C - avoid the naming conflicts and make the variables public (using a
convention for private and protected members).

I realised after my first post that using the private and privileges
functions in that way doesn't fit with the inhertiance model I'm using
either, so that makes it all academic.

As a side, and if you have time, could you take a look at the current
way I'm using JS to achieve OO features such as classes, inhertiance
and polymorphism? I'd appreciate your comments/thoughts on how I've
done things. I've included a (short) example web page here:

http://dev.kuluvalley.com/js/oo_example.html

Conventions I've used:

- private members prefixed with __ (double underscore)
- protected members prefixed with _ (single underscore)
- public members have no prefix
- my constructor functions (not js ones but oo-simulated ones) are
named __construct and are called last in the (js) constructor (sorry a
bit confusing with the terminology)

To simulate overriding/access to superclass functions I'm using a local
variable, __super, in each class as a holding place for a skeleton
object of the superclass whose (superclass) functions I call whilst
applying the current object as the execution context for the call.

I've found that using these tricks I can simulate all the OO features I
need without having to worry about prototype chains. Oh, and I don't
have the need to add members to my objects at runtime.

Thanks!
Rob

May 27 '06 #3

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

Similar topics

11
4567
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
6
4706
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
17
5493
by: Woody Splawn | last post by:
I am finding that time after time I have instances where I need to access information in a variable that is public. At the same time, the books I read say that one should not use public variables...
5
2660
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/callnetfrcom.asp The Joy of Interoperability Sometimes a revolution in programming forces you to abandon all...
37
2524
by: Joergen Bech | last post by:
(Slightly religious question): Suppose I have the following class: ---snip--- Public Class MyClass Private _MyVariable As Integer Public Property MyVariable() As Integer Get
2
1799
by: cdg | last post by:
Is there any way to directly access variables of one class from another class without passing any public member variables. Similar to how any function in a class can directly access (without...
8
5720
by: David Veeneman | last post by:
Should a member variable be passed to a private method in the same class as a method argument, or should the method simply call the member variable? For years, I have passed member variables to...
7
2174
by: Valeriu Catina | last post by:
Hi, consider the Shape class from the FAQ: class Shape{ public: Shape(); virtual ~Shape(); virtual void draw() = 0;
10
409
by: Les Desser | last post by:
In article <fcebdacd-2bd8-4d07-93a8-8b69d3452f3e@s50g2000hsb.googlegroups.com>, The Frog <Mr.Frog.to.you@googlemail.comMon, 14 Apr 2008 00:45:10 writes Thank you for that. It was very...
0
7120
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6991
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6878
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7373
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4583
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
649
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
286
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.