Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upAdding a method to Object.prototype causes error on ports #710
Comments
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
process-bot
Sep 12, 2016
Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it!
Here is what to expect next, and if anyone wants to comment, keep these things in mind.
process-bot
commented
Sep 12, 2016
|
Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it! Here is what to expect next, and if anyone wants to comment, keep these things in mind. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
lukewestby
Sep 12, 2016
Member
Some investigation notes:
Adding a property like this to Object.prototype is going to create a property on all objects that isn't an own property but is still enumerable. As a result, any time for .. in is used, including at the location of this error (https://github.com/elm-lang/core/blob/master/src/Native/Platform.js#L342), an enumerable property on Object.prototype is going to cause problems.
If this is something we want to fix here are a couple options with different tradeoffs:
- If an object in core is intended to be used as a dictionary or bag of some kind, create it with
Object.create(null). This creates a prototype-less object with no properties but its own.- Pro: no added performance cost during iteration, as the
for .. ins can stay how they are - Con: No support for IE8
- Pro: no added performance cost during iteration, as the
- Guard every iteration of a
for .. inloop withhasOwnProperty().- Pro: perfect browser support
- Con: has to be added in a lot of places and adds an additional function call to every iteration over an object's properties
|
Some investigation notes: Adding a property like this to If this is something we want to fix here are a couple options with different tradeoffs:
|
lukewestby
added
the
bug
label
Sep 12, 2016
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
halfzebra
Sep 26, 2016
Contributor
@lukewestby it must be possible to shim that with something like zloirock/core-js
Object.create(null) sounds like a better way to me.
|
@lukewestby it must be possible to shim that with something like zloirock/core-js
|
EuAndreh commentedSep 12, 2016
•
edited
Edited 1 time
-
EuAndreh
edited Sep 12, 2016 (most recent)
Whenever you try to add any functionality to
Object.prototype, Elm ports throws an error and stops working.This is a small reproducible example:
Run:
index.html file:
Served with:
It says:
And if the
Object.prototype.whateverline gets commented, everything goes back to working normally.After the error,
ports.sendstop working.