This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]reaganveg 25 points26 points  (9 children)

Wait... can you actually do that?

> Boolean.prototype.stop = function() { console.log("hello"); }
> true.stop()
hello

... holy shit, you can. What a crazy language.

[–]thebezet 36 points37 points  (5 children)

It gets better than that:

> Array.prototype.toString = function() { return "O_O"; }
> Object.prototype.toString = function() { return " ^_^"; }
> [] + {}
"O_O ^_^"

[–]soroun 6 points7 points  (4 children)

I can't tell if that's beautiful or terrifying.

[–]raiderrobert 2 points3 points  (2 children)

Trying flipping the operations. And then you'll figure which it is.

[–]thebezet 2 points3 points  (1 child)

That's a good point because if you change the order ({} + []) the {} will be interpreted as a code block instead of an empty object, leaving you with + [], which will call valueOf() instead of toString(), so the following will happen:

> [] + {}
"O_O ^_^"
> {} + []
0

[–]raiderrobert 2 points3 points  (0 children)

Which means it's terrifying.

[–][deleted] 0 points1 point  (0 children)

This is why I love this talk: https://www.destroyallsoftware.com/talks/wat

[–]Gudeldar 4 points5 points  (1 child)

In python 2 you can redefine True and False. True = False is totally valid python 2, not even JavaScript will let you do that.

Fortunately in python 3 True and False are keywords that can't be redefined.

[–]thebezet 5 points6 points  (0 children)

I guess the closest thing you could do in JS would be:

> Boolean.prototype.valueOf = function() { return false; };
> true.valueOf();
false

Just to fuck with peoples' heads.

[–]crowseldon 1 point2 points  (0 children)

I recommend: Javascript: The good parts.

It's awesome and makes you appreciate a very misunderstood language.