Closed Bug 696255 Opened 13 years ago Closed 12 years ago

object.prototype ruined with watch and unwatch

Categories

(Core :: JavaScript Engine, defect)

x86
macOS
defect
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 774645

People

(Reporter: smartj, Unassigned)

Details

User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:7.0.1) Gecko/20100101 Firefox/7.0.1 Build ID: 20110928134238 Steps to reproduce: Mozilla broke Prescreen.com by adding "watch" to the object prototype. Many JavaScript users will use {} as as key/value data-type. I used {} to store names of the elements I was putting inside of a collection. One of these elements happened to have a user-defined name of "watch." var elements = {}; ... if (!elements['watch']) { elements['watch'] = element; } Actual results: Native objects have "watch" and "unwatch" symbols attached to them, which breaks code. Expected results: There are millions of JavaScript programs that rely on {} to have a clean prototype. Deciding for these users that "watch" and "unwatch" are suitable "extensions" is a poor implementation decision. Mozilla is taking a gamble that this extremely common symbol name will not result in a namespace collision. If default extensions are required, please consider a namespace such as __SYMBOL__, which will not likely be used by ordinary users.
watch/unwatch are **** and on the way out. However, what you are doing is colliding with other standard names as well. The right way of using objects as a hash map is: var map = Object.create(null); That way you won't pollute your map with prototype properties. If you want to get rid of watch immediately, we should take that to the dev-platform mailing list. I think its hard because it will break web content. We have to do it slowly and carefully. The rest of your bug is more on your side I think, so I will close this as invalid. Use the code above instead.
Status: UNCONFIRMED → RESOLVED
Closed: 13 years ago
Resolution: --- → INVALID
I disagree w/ this resolution. Object.create was introduced in JavaScript 1.8.5, and I *could* use Object.create(null) if I knew that my scripts were always being invoked in JS 1.8.5. What is the suggested solution for the other 90% of users who are not running JavaScript 1.8.5? It seems to me that Object.create fixes the issue by just making it worse -- adding additional symbols to the root prototype and requiring detection code to work-around.
Status: RESOLVED → UNCONFIRMED
Resolution: INVALID → ---
Status: UNCONFIRMED → RESOLVED
Closed: 13 years ago12 years ago
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.