Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

private properties cannot be modified within the class #1

Closed
RobMayer opened this issue Aug 6, 2013 · 6 comments
Closed

private properties cannot be modified within the class #1

RobMayer opened this issue Aug 6, 2013 · 6 comments
Assignees

Comments

@RobMayer
Copy link

RobMayer commented Aug 6, 2013

I'm hitting an odd problem where private variables cannot be modified within methods of the class. is it possible I am doing something wrong?

var theClass = jClass.extend({
   init: function() {
      this.__privateProperty = 0;
   },
   increase: function() {
      this.__privateProperty++;
   },
   show: function() {
      return this.__privateProperty;
   }
});

var foo = new theClass();
foo.increase();
foo.show(); //returns 0 instead of 1

I have no idea if I am doing something wrong or not, but if I'm not, then this here bug is a problem.

@RobMayer
Copy link
Author

RobMayer commented Aug 6, 2013

... nevermind - I'm a moron.
to reference a private variable within the class, you need to can also use this syntax

this.__.privateProperty = "FooBar";

@riga
Copy link
Owner

riga commented Aug 6, 2013

You're not a moron ;)
You indeed found a bug, that I just fixed, since your example from above MUST work.
In your 2. post you actually use the private object which is another way of implementing private members.
It's slightly faster because the approach is quite different. You can use both ways, but if you plan to only use the private object you may want to disable tracking.

@riga riga closed this as completed in 1db6e78 Aug 6, 2013
@ghost ghost assigned riga Aug 6, 2013
@RobMayer
Copy link
Author

RobMayer commented Aug 6, 2013

Supposing I use the private object, disable tracking, I would then declare all private methods and properties as follows?

var theClass = jClass.extend({
   init: function() {
      this.__.privateProperty = 0;
   },
   publicMethod: function() { //public method
      this.__.privateProperty++; //private property reference
   },
   __.privateMethod: function() {
      this.__.privateProperty++;
   }
});

@riga
Copy link
Owner

riga commented Aug 6, 2013

The definition of privateProperty is correct, but your privateMethod won't work.
The private Object (this.__) is only visible inside the scope of your class functions. Functions defined in the private Object itself obviously won't have all the inheritance benefits. These two examples will work:

var theClass = jClass.extend({
   init: function() {
      this.__.privateProperty = 0;
   },
   publicMethod: function() { //public method
      this.__.privateProperty++; //private property reference
   },
   show: function() {
      return this.__.privateProperty;
   }
}, {tracking: false});
var theClass = jClass.extend({
   init: function() {
      this.__privateProperty = 0;
      this.__.anotherPrivateProperty = "foo";
   },
   __privateMethod: function() {
      this.__privateProperty++;
      this.__.anotherPrivateProperty = "bar"
   },
   show: function() {
      console.log(this.__privateProperty, this.__.anotherPrivateProperty);
   }
});

Note that private members defined with two underscores (defined in privatePattern) in their name and private members defined within the private Object are two different approaches that can be used at the same time.

Btw: I updated the npm package, so your first example should work now.

@RobMayer
Copy link
Author

RobMayer commented Aug 7, 2013

I am unsure if this is related to the fix you had made or not, but...

var theClass = jClass.extend({
    init: function() {
        this.__privateProperty = 0;
    },

    increase: function() {
        this.__privateProperty++;
    },

    show: function() {
        return this.__privateProperty;
    }
});

var exA = new theClass();
var exB = new theClass();

exA.increase();
exB.show(); //expect 0, returns 1

@riga
Copy link
Owner

riga commented Aug 9, 2013

Thanks for reporting this issue (which is indeed a big one).
I opened a new ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants