-
Notifications
You must be signed in to change notification settings - Fork 29
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
Comments
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. |
for private methods, I suggest making |
In your example, I would try crud.init = function() {
var self = this;
$(document).on('click', function() { alert(self.container) });
}; (note the additional |
That way the |
Yes, thanks, I have got that. do you think is that possible? |
I'm not sure what you mean. |
Think I have got it, please take a look Please correct me of any concept misunderstanding :) |
P.js already doesn't require you to use |
@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". |
@ThiagoF so I think what you are trying to accomplish is Java-style private instance variables, yes? |
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 |
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:
|
Consider the following classes
All fine. Loved the implementation.
Lets go to the problem, thats occours on instantiation
The private
self
, used to reference the Class out of thethis
scope, got overwritten by each instance. So the events don't knowUserPage
anymore.The text was updated successfully, but these errors were encountered: