-
-
Notifications
You must be signed in to change notification settings - Fork 173
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
Fix Array.prototype
overwrites breaking for...in
loops
#6135
Fix Array.prototype
overwrites breaking for...in
loops
#6135
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's one of those decisions that we need to undo at some point. Your solution seems great to me. @distantnative what do you think?
Wondering if we should already use this moment t move these to normal helper functions, then use object.defineProperty as suggested to add them in a usable way to the Array object, but mark that already as deprecated. |
I thought so as well – why not make them importable utils like all the other helpers (debounce etc.). |
The base branch was changed.
Do you have an idea why this doesn't seem to work? export function split() {
//...
}
Object.defineProperty(Array.prototype, "split", {
value: split,
enumerable: false, // This makes sure the property is not enumerable
writable: true,
configurable: true
}); |
Mmh, I guess because the |
Yes, the first parameter must be the array then. But somehow still didn't work for me |
Ok I just had a stupid typo somewhere that completely threw me off. What do you think of 48fe180? |
48fe180 looks fine. Great solution while being backwards-compatible. 👏 Regarding Edit: Feel free to close this PR in favor of your commit. |
Closing in favor of #6140 |
This PR …
… wishes the team a happy new year! While fixing a bug.
I'm developing a new Kirby Panel plugin, which uses a third-party JavaScript library. This library itself uses the
for...in
loop. The Panel itself adds two methods to theArray.prototype
:sortBy
andsplit
, leading to the following error in my third-party library:Modifying
Array.prototype
or any built-in prototype is generally considered a bad practice, but who cares. 🙂 Unfortunately, I can't change the third-party library and need a fix.When you add properties to
Array.prototype
, they become enumerable, which means they will show up infor...in
loops that iterate over arrays. This can lead to errors and bugs, especially if other parts of the codebase are not expecting these additional properties. This isn't relevant for the Panel, but Panel plugins running in the same thread.Since the Panel has to add methods to
Array.prototype
and we want to avoid this error, we have another option:Object.defineProperty
: This way, they won't appear infor...in
loops.My plugin depends on the third-party library, so development is kinda blocked at the moment. If I may have a wish for the new year, it's the merge of this PR. Thanks in advance!
Fixes
Any JavaScript code that uses
for...in
under the hood.Breaking changes
None.
Ready?
For review team