-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
[Mobx >= 4] Object.hasOwnProperty fails #1526
Comments
@rennerDa although weird, this behavior is expected and cannot be robustly fixed; What MobX does is moving the property to the own instance as soon as it is read or written for the first time. That is the best we can do with the current implementation of the decorators transformer (the stage 2 proposel fixes this issue). The reason it probably worked in 3.6 is the fact that |
I just run the above test against MobX 3.6, and it yields the same results. There might be a subtle difference, but the above example doesn't expose it at least. In MobX 3.6 the log returns |
(added cannot reproduce; the issue can be reproduced, but cannot reproduce that it changed from MobX 3) |
Thanks for you fast reply. You are absolutely right (sorry, should've tested more). I had to extend the TodoModel a little bit to get it working with Mobx 3.x:
This works in 3 but not with mobx 4.2.0:
It works because of the
Did you change the way actions and observables were assigned to objects? Thanks in advance for every hint/help :) . |
Yes, some stuff changed regarding the initialization of decorators to make it more consistent across platforms. The effect might be that the semantics have become less optimal in certain cases. The general way to avoid this issue is to read the properties first before reflecting on them (reading / writing will actually cause the properties to move from the prototype to the instance) Alternatively, since this is mainly a decorator issue (decorators in stage-0 cannot attach behavior to instances, only to prototypes, that is where the lazy behavior originates from), the work around is to use |
I am also running into this on 3.6.2 and 4.0.0.
Is this the expected behavior? As you can see, my fix is to wrap the object in |
Yes, the behavior is as expected @aaronatmycujoo, and cannot be changed without proxies (that is Mobx < 5) |
Also, this thread if about objects. Arrays are different data structures, for follow up questions, please open a separate issue. |
Closing for now, will be reopened once #1928 happens. |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs or questions. |
I don't know if this is a bug in mobx or babel or anything else.
I tested this with MobX 3.6 and earlier versions and it worked. It breaks with Mobx >= v4.
I uses this repository (https://github.com/mobxjs/mobx-react-boilerplate) and changed the following files:
.babelrc
package.json
When I try to check if the Model has a property with "hasOwnProperty" the result is false (if nothing was assigned before):
TodoModel.js
If I remove the comment and assign title in front (
this.title = title;
), hasOwnProperty is true.This is maybe a bad example, but it shows my problem.
The text was updated successfully, but these errors were encountered: