Object.prototype is erlaubt
It just occurred to me that the
forEach
method I introduced on Thursday allows us to extend Object.prototype
:
// extend Object.prototype Object.prototype.VERBOTEN = "Object.prototype is verboten!"; // create an object var robot = { name: "Bender", occupation: "bender" }; // iterate over the object's properties in the usual way for (var i in robot) print(robot[i], i); // => VERBOTEN: Object.prototype is verboten! // => name: Bender // => occupation: bender // iterate over the object's properties using the forEach method forEach (robot, print); // => name: Bender // => occupation: bender
Of course, extending Object.prototype
is still
verboten.
Just thought it was interesting.
Comments (20)
Leave a comment
Comment: #1
shouldn’t this be
Just to be pedantic
Can you explain why the behaviour you describe happens?
Comment: #2
always interesting dean. are these issues you discover from time to time being addressed in future js(2) specs? from what i can tell from your posts, there simply is no safe hash available to programmers in js.
Comment: #3
Hm, I had to think about it quite a while, but I think, I understand now. Really cool
Comment: #4
@Christof – thanks for catching the wonky code! And I’m glad that the penny has dropped for you.
@dude – JS2 fixes a lot of these annoyances. It will probably introduce some new ones though.
Comment: #5
@Dean: Glad you like my headline so much that you chose to reuse it: http://www.thomasfrank.se/object_prototype_is_erlaubt.html
And that you give yet another example that shows it is not a problem to extend the Object.prototype.
Comment: #6
@Thomas – I did like your headline.
I still think it is a bad idea to extend
Object.prototype
though.Comment: #7
@Thomas: The worst thing about your Blog is that it tries to dictate which size my Browser window should be.
Comment: #8
@Christof:
The worst thing about yours is that it is in German ;-). No, but seriously – almost no web page is immune to size – your name get cut of if I scale your blog. However I’m working on a more floating layout as an alternative. My site/blog is only two months old, it’s a baby… Have patience. When it gets middle aged (1-2 years old) I guess every element will float in the most mind boggling way.
Comment: #9
@Thomas: The problem is not, that the content on your page has a fixed width, or whatever. The problem is, that it resizes my Window on load which is very annoying. Simply cut out that part from your Javascript and most people will simply be happy with your Blog.
Comment: #10
@Dean: Sorry we’re totally off topic here.
@Christof: Damn! You’re right! I do resize by 1 px and then after a short timeout resize back. I forgot about this. In my javascript I’d made a comment saying this was to fix som strange css bug in Firefox. Probably Firefox <1.5.
I commented it and can’t detect any anomalies in Firefox 1.5… But I’m curious what this solved originally… Well thanks for pointing it out to me! Hope you’re right about most people being happy with my blog now – seems like an easy way to please a crowd.
Comment: #11
Can you please tell me how the headline look like on your browser?
On my one, it is a mixture of english and german…
“Object.prototype is erlaubt”
Is it a sideeffect of a script you are using? Or is it really the original title?
Bye, Markus
Comment: #12
@Markus: No, there is a special script that translates part of the content just for German visitors. It tries to detect if the person sitting in front of the computer is able to understand German. If not, it remains untranslated. Same goes with this article.
Comment: #13
lol
Comment: #14
@Markus, @Konstantin: like this:
You just need jQuery installed
Comment: #15
Finally I can see the <span id=”enReason” style=”display:none”>reason</span><span id=”deReason” style=”display:inline”>Ursache</span> for using jQuery.
Comment: #16
The
$()
-Function I used is the one jQuery provides. That is needed to haveb.html()
.Comment: #17
This dEnglish is the strangest thing…
Comment: #18
I think somebody missed the joke
Comment: #19
Because you are quoted now on so many sites, we should try to introduce the words erlaubt and verboten in modern English. This language is not so static over time anyway
Comment: #20
Our site is dEnglish as (w|h)ell, but at least it really scales to fit the browser window – even the text size!
Only thing that drives us crazy is that js code. How can we parse Strings that are members of an enum in an array of that enums? Just in case…
And, @Kevinin: Yes, we got the joke…
Comments are closed.