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 Variables dont match the Instance #31

Open
macedd opened this issue Dec 5, 2014 · 12 comments
Open

Private Variables dont match the Instance #31

macedd opened this issue Dec 5, 2014 · 12 comments

Comments

@macedd
Copy link

macedd commented Dec 5, 2014

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.

@jneen
Copy link
Owner

jneen commented Dec 5, 2014

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
Copy link
Owner

jneen commented Dec 5, 2014

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

@jneen
Copy link
Owner

jneen commented Dec 5, 2014

In your example, I would try

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

(note the additional var)

@jneen
Copy link
Owner

jneen commented Dec 5, 2014

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

@macedd
Copy link
Author

macedd commented Dec 5, 2014

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?

@jneen
Copy link
Owner

jneen commented Dec 5, 2014

I'm not sure what you mean.

@macedd
Copy link
Author

macedd commented Dec 5, 2014

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

Please correct me of any concept misunderstanding :)

@jneen
Copy link
Owner

jneen commented Dec 5, 2014

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

@laughinghan
Copy link
Contributor

@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".

@jneen
Copy link
Owner

jneen commented Dec 6, 2014

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

@jneen
Copy link
Owner

jneen commented Dec 6, 2014

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 _.

@macedd
Copy link
Author

macedd commented Dec 7, 2014

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 notifications@github.com 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:
#31 (comment)

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

3 participants