mobx: [5.10.1] Extending Array.prototype – `Cannot read property 'value' of undefined`

Hi there!

TL;DR: code sample

I have a:

  • Issue:
  • Provide error messages including stacktrace
mobx.module.js:2199 Uncaught TypeError: Cannot read property 'value' of undefined
    at extendObservableObjectWithProperties (mobx.module.js:2199)
    at Function.object (mobx.module.js:542)
    at ObservableValue.deepEnhancer [as enhancer] (mobx.module.js:378)
    at ObservableValue../node_modules/mobx/lib/mobx.module.js.ObservableValue.prepareNewValue (mobx.module.js:747)
    at ObservableObjectAdministration../node_modules/mobx/lib/mobx.module.js.ObservableObjectAdministration.write (mobx.module.js:3871)
    at Test.set [as data] (mobx.module.js:4094)
    at Module../src/test.tsx (test.tsx:34)
    at __webpack_require__ (bootstrap:19)
    at Module../src/index.tsx (index.tsx:4)
    at __webpack_require__ (bootstrap:19)
  • Provide a minimal sample reproduction. Extending Array.prototype – MobX 5.10.1

  • Did you check this issue wasn’t filed before?

  • Elaborate on your issue. What behavior did you expect? Seems like this issue relates to extending Array.prototype. Having such extension is not a must in most project, and even might be not safe, but with Typescript (which I use) I think such a case would be common.

  • State the versions of MobX and relevant libraries. Crashes on 5.10.1, doesn’t crash on 5.9.4.

Thanks!

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 18 (9 by maintainers)

Commits related to this issue

Most upvoted comments

@idudinov You have to use defineProperty to set enumurable: false so it won’t be seen when looping over it. In contrast, that makes it less elegant to use so you can start refactoring to a more supported way 😃

@loklaan tested, I don’t see this error anymore

The reason why extending prototypes is not safe is mainly because the language implementer might decide to add a method named exactly like yours some day.

edit: TypeScript might also give you a false sense of security - if you forget to run the prototype assignment in an iframe for example, TS could think you have the method but you wouldn’t since iframes have their own array constructor (different realm)

Added a guard that extendObservable doesn’t accept inheritance based property objects, as changing the behavior of getPlainObject keys sounds potentially more dangerous (it is also used by other api’s to reflect on user input)