开发者

How can I break the law of non-contradiction in Javascript?

The law of non-contradiction dictates that two contradictory statements cannot both be true at the same time. That means that the expressions

(a && !a)
(a == !a)
(a === !a)

should always evaluate to a falsy value, and

(a || !a)

should always evaluate to a truthy value.

Fortunately, though, Javascript is a fun language that allows you to do all sorts of sick things. I bet someone a small fortune that it's possible to convince Javascript to break the law of non-contradiction, or, at lea开发者_Python百科st, convincingly make it look like it's breaking the law of non-contradiction. Now I'm trying to make all four of the above code examples give the unexpected result.

What would be a good way to go about this?


The best I can do is:

[] == ![] // true

or

var a = []; 
a == !a

Of course this is really doing [] == false // true and !![] == ![] // false. It's really just a technicality.

EDIT: This is really a joke, but does work:

var a = false; var b = function() { return a = !a };
console.log(!!(b() && !b())); // true
console.log(b() == !b()); // true
console.log(b() === !b()); // true
console.log(b() || !b()); // true


This one will do the trick:

var a = '0';
a == !a

(evaluates to true)

In this case, a == false and !a == false.


a=NaN;

var a=NaN, 
A=[(a && !a), (a == !a),(a === !a),(a || !a)];
alert(A)

/*  returned value: (Array)
NaN,false,false,true
*/


I still haven't found anything to break && and ===, but here's one for == and ||:

Object.prototype.toString = function() {
    return false;
};
a = {};
b = (a || !a);
alert(a || !a);  //alerts false
alert(b == !b);  //alerts true
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜