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

Fix console error when using custom Array prototype functions #1682

Merged
merged 1 commit into from
Oct 2, 2024

Conversation

mccahan
Copy link
Contributor

@mccahan mccahan commented Sep 27, 2024

If you've added a function to the Array prototype, using for (value in values) will iterate through the functions attached to the prototype in addition to the values, which can throw a console error. Instead, we should use for (value in values.values()).

To reproduce this issue:

  • Create a new blank Laravel project (composer create-project laravel/laravel debugbar-test && cd debugbar-test)
  • Install Debugbar (composer require barryvdh/laravel-debugbar --dev)
  • Add an Array prototype function to resources/views/welcome.blade.php:
<script>
            Array.prototype.customRemove = function(item) {
                const i = this.indexOf(item)
                if (i > -1) {
                    this.splice(i, 1)
                }
                return this
            }
        </script>
  • php artisan serve and load in a browser
  • Observe the console error:
    image

Prevent issues when the user has added a function to the Array prototype
@mccahan mccahan changed the title Use Array.values() for iteration Fix console error when using custom Array prototype functions Sep 27, 2024
@barryvdh barryvdh merged commit 69b49c1 into barryvdh:master Oct 2, 2024
25 checks passed
@mccahan mccahan deleted the patch-1 branch October 2, 2024 13:24
mccahan added a commit to mccahan/laravel-debugbar that referenced this pull request Oct 9, 2024
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

Successfully merging this pull request may close these issues.

2 participants