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

for...in iterating through inherited properties #7402

Open
5 tasks done
gquagliano opened this issue Jan 10, 2021 · 4 comments
Open
5 tasks done

for...in iterating through inherited properties #7402

gquagliano opened this issue Jan 10, 2021 · 4 comments
Labels
compatibility Cross-browser/device/environment compatibility

Comments

@gquagliano
Copy link

Hello. I've noticed that there are many for...in loops that iterate over all properties, including inherited ones. This leads to errors when some other script has added properties to the prototype. For instance, I ran into this error when I tried to create a map in a page where other script had added an extra property to Object's prototype:

TypeError: Cannot read property 'fn' of undefined at NewClass._on (leaflet-src.js:510)

Adding properties to native prototypes may be a bad practice, but sometimes you don't have control over it. And it's also a good practice to use hasOwnProperty with for...in loops.

Should I push these validations? I added them in my local copy and the app is working fine now.

  • I've looked at the documentation to make sure the behavior is documented and expected
  • I'm sure this is a Leaflet code issue, not an issue with my own code nor with the framework I'm using (Cordova, Ionic, Angular, React…)
  • I've searched through the issues to make sure it's not yet reported

Steps to reproduce
Steps to reproduce the behavior:

  • Alter Object prototype (Object.prototype.test="test";)
  • Create a new map (L.map())

Expected behavior
Map should be created normally.

Current behavior
Fails due to the unexpected property test in Object.

Environment

  • Leaflet version: 1.7.1
  • Browser (with version): Opera 73.0
  • OS/Platform (with version): Windows 10

Additional context
See intro.

Minimal example reproducing the issue

https://plnkr.co/edit/iLQ7Mlm5tbvZMgyv

  • this example is as simple as possible
  • this example does not rely on any third party code
@johnd0e
Copy link
Collaborator

johnd0e commented Jan 10, 2021

And it's also a good practice to use hasOwnProperty

Or Object.keys(...).forEach(function(...) { (if we do not care about IE<9).

But I also should mention that there is another good practice - do not extend Object prototype.

@gquagliano
Copy link
Author

That's exactly what I said too, but what if you don't have control over it (it's done by other library that you need)? Isn't this a case worth considered? Specially given how simple the solution is. Thank you for your time and reply.

@johnd0e
Copy link
Collaborator

johnd0e commented Jan 10, 2021

Oh, forgot to mention that direct use of hasOwnProperty is not good practice too: preferable form is Object.prototype.hasOwnProperty.call(this, ...).

Specially given how simple the solution is.

It is not that simple, as it requires a hundreds of changes.
And what is also important (as for me): additional wrapping in extra if adds visual complexity to very trivial pieces of code.

what if you don't have control over it (it's done by other library that you need)?

Well, are you sure that it is really unique lib, and you are not able to replace it?
Even if you adapt Leaflet, there may occur some another incompatible dependency (incl. Leaflet's plugin) that is also out of your control.

@gquagliano
Copy link
Author

Ok. It's clear, thank you for your insight.

@Malvoz Malvoz added the compatibility Cross-browser/device/environment compatibility label Jun 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compatibility Cross-browser/device/environment compatibility
Projects
None yet
Development

No branches or pull requests

3 participants