pjs icon indicating copy to clipboard operation
pjs copied to clipboard

Private Variables dont match the Instance

Open macedd opened this issue 10 years ago • 12 comments

Consider the following classes

    var CrudPage = P(function(crud)
    {
        var self = crud;
        crud.container = '#none';

        crud.init = function() {
            self = this;

            $(document)
                .on('click', function()
                {
                    alert(self.container);
                });
        }
    }
    var UserPage = P(CrudPage, function(user, crud)
    {
        movimentacao.container = '#user';
    });
    var ProductPage = P(CrudPage, function(product, crud)
    {
        movimentacao.container = '#product';
    });

All fine. Loved the implementation.

Lets go to the problem, thats occours on instantiation

# we create the instances
var user = UserPage();
var product = ProductPage();

# and trigger our test click
$(document).trigger('click');

The private self, used to reference the Class out of the this scope, got overwritten by each instance. So the events don't know UserPage anymore.

macedd avatar Dec 05 '14 19:12 macedd

I'm not sure what you're trying to do, but any var you put in the definition function is gonna be class-scoped, not instance-scoped.

jneen avatar Dec 05 '14 21:12 jneen

for private methods, I suggest making self a parameter and passing in this from public methods.

jneen avatar Dec 05 '14 21:12 jneen

In your example, I would try

crud.init = function() {
  var self = this;
  $(document).on('click', function() { alert(self.container) });
};

(note the additional var)

jneen avatar Dec 05 '14 21:12 jneen

That way the self variable isn't shared with every instance

jneen avatar Dec 05 '14 21:12 jneen

Yes, thanks, I have got that. But trying currently to overlap P in order to create a new instance (for the cases it is convenient).

do you think is that possible?

macedd avatar Dec 05 '14 22:12 macedd

I'm not sure what you mean.

jneen avatar Dec 05 '14 22:12 jneen

Think I have got it, please take a look https://gist.github.com/thiagof/01bcbab8f2041fa26a7b

Please correct me of any concept misunderstanding :)

macedd avatar Dec 05 '14 22:12 macedd

P.js already doesn't require you to use new... I'm not sure what the gain there is.

jneen avatar Dec 05 '14 22:12 jneen

@thiagof: just to be clear,

    var CrudPage = P(function(crud)
    {
-       var self = crud;
        crud.container = '#none';

        crud.init = function() {
-           self = this;
+           var self = this;

            $(document)
                .on('click', function()
                {
                    alert(self.container);
                });
        }
    }
    var UserPage = P(CrudPage, function(user, crud)
    {
        movimentacao.container = '#user';
    });
    var ProductPage = P(CrudPage, function(product, crud)
    {
        movimentacao.container = '#product';
    });

does solve your problem, right? Then, what cases do you think your idea, even if you got it to work, would be more convenient? In this case, for example, our suggested canonical way to use Pjs is actually less code.

I think @jneen and I have no idea what you mean by "overlap P in order to create a new instance".

laughinghan avatar Dec 05 '14 23:12 laughinghan

@thiagof so I think what you are trying to accomplish is Java-style private instance variables, yes?

jneen avatar Dec 06 '14 01:12 jneen

P.js does not support these, and intentionally so. Your solution would involve creating a new class for every instantiation of an object, which I'd rather not do in Pjs. If I need private member variables, I usually prefix their names with _.

jneen avatar Dec 06 '14 01:12 jneen

I think problem migth be with javascript closure scope variables - i do not got my idea to work.

Yes, what works is redefine self in each function a event is registred (creating the closure).

Create isolate class instances would be benefical for designing a more reliable class isolation pattern (when needed). But or this isnt possible with javascript or I didnt have luck with cloning the objects before proto creation.

Han [email protected] escreveu:

@thiagof: just to be clear,

crud.init = function() {
 var self = this;
 $(document).on('click', function() { alert(self.container) });
};

and omitting the var self = crud; does solve your problem, right? Then, what cases do you think your idea, even if you got it to work, would be more convenient?


Reply to this email directly or view it on GitHub: https://github.com/jneen/pjs/issues/31#issuecomment-65873305

macedd avatar Dec 07 '14 07:12 macedd