473,749 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
+ 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(xLoca l)
{
x = xLocal;
}

this.setY(yLoca l)
{
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 3987
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(xLoca l)
{
x = xLocal;
}

this.setY(yLoca l)
{
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
4613
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 variables". However, this doesn't seem entirely correct. It also doesn't mention whether static member functions can access protected and private member data and methods (and I couldn't spot this in the FAQ). I have a class row<Row> which derives from...
6
4751
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 appreciated. Thanks in advance
17
5533
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 too much - that it's bad programming practice. Is there an easy way to deal with this? I would like to do things in the "Best Practices" way but at the same time I don't want to make a federal case out of it. This comes up over and over...
5
2688
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 that's come before. To take an extreme example, suppose you have been writing Visual Basic applications for years now. If you're like many developers, you will have built up a substantial inventory of code in that time. And if you've been following...
37
2601
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
1817
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 passing an argument) any member variable of its' own class. I thought the only way this could be possible would be to make a function a "friend" of the class. But I now beleive that it is still necessary to pass variables to the other class, is this...
8
5742
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 private methods in the same class as method arguments. For example, if Public method Foo() calls private method Bar(), and if Bar() uses member variable m_MyVariable, I declare the private method with the signature: private void Bar(SomeType...
7
2184
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 clear and I actually understand it!
0
8996
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9566
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9333
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8256
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6800
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6078
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3319
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.