dean.edwards.name/weblog/2006/07/erlaubt/

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

// => verbotenProperty: Object.prototype is verboten!

shouldn’t this be

// => VERBOTEN: Object.prototype is verboten!

Just to be pedantic:-)

Can you explain why the behaviour you describe happens?

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 by: crunky dude
  • Posted:

Hm, I had to think about it quite a while, but I think, I understand now. Really cool:-)

@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 by: -dean
  • Posted:

@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.

@Thomas – I did like your headline.:-)

I still think it is a bad idea to extend Object.prototype though.

  • Comment by: -dean
  • Posted:

@Thomas: The worst thing about your Blog is that it tries to dictate which size my Browser window should be.

@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.

@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.

@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.;-)

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 by: Markus
  • Posted:

@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 by: Konstantin
  • Posted:

lol

  • Comment by: -dean
  • Posted:

@Markus, @Konstantin: like this:

function translate(map) {
  var b = $('body')
  var text = b.html();
  map.forEach( function(pair) { text.replace(pair[0],pair[1]); });
  b.html(text);
}
if(user.languages[de]) 
  translate([[/forbidden/,'verboten'],[/permitted/,'erlaubt']]);

You just need jQuery installed:-)

Finally I can see the <span id=”enReason” style=”display:none”>reason</span><span id=”deReason” style=”display:inline”>Ursache</span> for using jQuery.

;-)

The $()-Function I used is the one jQuery provides. That is needed to have b.html().

This dEnglish is the strangest thing…

  • Comment by: Kevinin
  • Posted:

I think somebody missed the joke;)

  • Comment by: Kevinin
  • Posted:

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 by: Rainer
  • Posted:

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.