Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Private instance members

Reply
Thread Tools

Private instance members

 
 
Will
Guest
Posts: n/a
 
      10-24-2003
Is it possible to have private instance members in a javascript class?

function myObject() {
var private = 1;

this.getPrivate = function() {
return private;
}

this.incrementPrivate = function() {
private += 1;
}
}

var a = new myObject();
var b = new myObject();
a.incrementPrivate();

When I test the example above, calling b.getPrivate() returns 2, so it
appears that private members can only be static? I know that it is
possible to use "this.private" to achieve the intended functionality
with a public member but would prefer to make the variable "private"
inaccessible to code outside the class.
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      10-24-2003


Will wrote:

> Is it possible to have private instance members in a javascript class?
>
> function myObject() {
> var private = 1;
>
> this.getPrivate = function() {
> return private;
> }
>
> this.incrementPrivate = function() {
> private += 1;
> }
> }
>
> var a = new myObject();
> var b = new myObject();
> a.incrementPrivate();
>
> When I test the example above, calling b.getPrivate() returns 2, so it
> appears that private members can only be static? I know that it is
> possible to use "this.private" to achieve the intended functionality
> with a public member but would prefer to make the variable "private"
> inaccessible to code outside the class.


Which browser are you using?
private is a reserved word I think therefore Netscape gives an error and
shouldn't yield a result at all.
And if I try

function myObject() {
var private = 1;

this.getPrivate = function() {
return private;
}

this.incrementPrivate = function() {
private += 1;
}
}

var a = new myObject();
var b = new myObject();
a.incrementPrivate();
alert(b.getPrivate())

with IE6 it alerts 1.
--

Martin Honnen
http://JavaScript.FAQTs.com/

 
Reply With Quote
 
 
 
 
Douglas Crockford
Guest
Posts: n/a
 
      10-24-2003
> function myObject() {
> var private = 1;
>
> this.getPrivate = function() {
> return private;
> }
>
> this.incrementPrivate = function() {
> private += 1;
> }
> }
>
> var a = new myObject();
> var b = new myObject();
> a.incrementPrivate();
>
> When I test the example above, calling b.getPrivate() returns 2, so it
> appears that private members can only be static? I know that it is
> possible to use "this.private" to achieve the intended functionality
> with a public member but would prefer to make the variable "private"
> inaccessible to code outside the class.


I ran it through JSLINT and it found that you are misusing 'private', a
reserved word.

http://www.crockford.com/javascript/lint.html


 
Reply With Quote
 
Will
Guest
Posts: n/a
 
      10-25-2003
Sorry for the confusion. The previous example was thrown together for
demonstration purposes and I should have remembered about "private"
being a reserved word.

The problem actually relates to private instance variables with a
prototyped class. For example:

function myObject() {
var i = 1;
this.ret = function() {return i;}
this.increment = function() {i += 1;}
}

myObject2.prototype = new myObject();
function myObject2() {}

var a = new myObject2();
var b = new myObject2();
a.increment();
alert(b.ret())

In this example,b.ret() returns 2 where I would expect 1. It appears
as though both objects "a" and "b" share an instance of the prototyped
class "myObject()", because the methods of each object alter the value
of the private variable for both.

I find this behaviour a bit odd and wonder if there is either
something I am missing or a workaround to enable the two objects to
maintain the variable "i" independently of one another whilst keeping
"i" as a private variable.
 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      10-25-2003


Will wrote:

> Sorry for the confusion. The previous example was thrown together for
> demonstration purposes and I should have remembered about "private"
> being a reserved word.
>
> The problem actually relates to private instance variables with a
> prototyped class. For example:
>
> function myObject() {
> var i = 1;
> this.ret = function() {return i;}
> this.increment = function() {i += 1;}
> }
>
> myObject2.prototype = new myObject();
> function myObject2() {}
>
> var a = new myObject2();
> var b = new myObject2();
> a.increment();
> alert(b.ret())
>
> In this example,b.ret() returns 2 where I would expect 1. It appears
> as though both objects "a" and "b" share an instance of the prototyped
> class "myObject()", because the methods of each object alter the value
> of the private variable for both.
>
> I find this behaviour a bit odd and wonder if there is either
> something I am missing or a workaround to enable the two objects to
> maintain the variable "i" independently of one another whilst keeping
> "i" as a private variable.


The beahviour is not odd, and your explanation is quite right, the two
objects share an "instance of myObject" as their prototype, that is how
prototyping works, an object is created and serves as the prototype.
Whoever has sold this approach of "private instance members" should have
told you that it breaks with prototypes being introduced.
--

Martin Honnen
http://JavaScript.FAQTs.com/

 
Reply With Quote
 
Will
Guest
Posts: n/a
 
      10-25-2003
Please to report that I have now found the answer, thanks to this excellent article:

http://www.pbwizard.com/Articles/class_inheritance.htm

Well worth a read.
 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      10-28-2003
"Martin Honnen" <> wrote in message
news:3f9a4015$...
<snip>
>Whoever has sold this approach of "private instance members"
>should have told you that it breaks with prototypes being
>introduced.


As Douglas Crockford appears to be responsible for inventing the
technique for emulating private instance members in JavaScript (though I
can't see that as "selling") it is probably not surprising that he has
also published pages on alternative approaches to inheritance, some of
which would address this problem.

As the emulation of private instance members is achieved by forming a
closure, in which the private members are stored, by assigning inner
functions of the constructor to public members of the object instance,
the technique results in each object instance being associated with a
closure. Assigning an super class object instance to the prototype
results in only one closure being associated with all instances of the
subclass, but the subclass instances do not each have a closure of their
own to hold any private instance members that they may want. Attaching a
closure to the prototype is an action that can be exploited as one of
the methods of emulating private static members (the other (and perhaps
preferable) method being to associate a closure with the class
constructor).

For instances of a class to inherit private instance member
functionality from a superclass they would have to explicitly create the
same closure as the superclass, which could be achieved by using the
Function.prototype.apply or call methods to apply the superclass
constructor to the - this - object (probably within the subclass
constructor).

Richard.


 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      10-28-2003


Richard Cornford wrote:
> For instances of a class to inherit private instance member
> functionality from a superclass they would have to explicitly create the
> same closure as the superclass, which could be achieved by using the
> Function.prototype.apply or call methods to apply the superclass
> constructor to the - this - object (probably within the subclass
> constructor).


Sure, but apply/call only made it into IE5.5/JScript 5.5 and are not
supported in earlier JScript versions so in my view can't be relied on
currently on Web pages.

--

Martin Honnen
http://JavaScript.FAQTs.com/

 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      10-28-2003
"Martin Honnen" <> wrote in message
news:3f9e801f$...
<snip>
>>... , which could be achieved by using the
>>Function.prototype.apply or call methods to apply the
>>superclass constructor to the - this - object (probably
>>within the subclass constructor).

>
>Sure, but apply/call only made it into IE5.5/JScript 5.5 and
>are not supported in earlier JScript versions so in my view
>can't be relied on currently on Web pages.


True, they are missing from IE 4 & 5.0, but they can each be emulated
when absent, and that addresses web page reliability issues (obviously
at the cost of maybe 20 extra lines of code).

On the other hand I have not seen much need for inheritance in a web
page environment, they are just not that complicated. Where I do see
inheritance being a significant consideration is with the implementation
of business logic in an ASP/IIS environment. But then the JScript
version is known so reliability is not an issue.

Richard.


 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-23-2003
Will wrote:
> Is it possible to have private instance members in a javascript class?


No, and unless you are talking about JavaScript 2.0 there are no classes
in JavaScript. The below declares and defines a constructor function
for a prototype object and therefore that object itself.

> function myObject() {
> var private = 1;
>
> this.getPrivate = function() {
> return private;
> }
>
> this.incrementPrivate = function() {
> private += 1;
> }
> }
>
> var a = new myObject();
> var b = new myObject();
> a.incrementPrivate();


The `new' operator creates a new object based on the prototype
object (inheriting its properties) and returns a reference to
that object which you assign to variables here, making their
identifiers object references on which the lookup operator `.'
can be applied.

Since in prototype-based languages like JavaScript 1.x every
object is an `instance', it is better to avoid that term there
to avoid confusion.

See
http://devedge.netscape.com/library/...2.html#1008342


PointedEars
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
class members vs instance members hdixon Python 3 07-09-2006 06:56 PM
Difference between static final members and final static members(if any)? JFCM Java 4 02-07-2006 11:32 AM
Which members are created automatically, which members are not inherited? lovecreatesbeauty C Programming 43 02-06-2006 11:36 PM
Templates: Members Vs. non-members Dave C++ 3 08-10-2004 11:23 AM
Can nested class members access private members of nesting class? CoolPint C++ 8 12-14-2003 02:30 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57