http://alf.nu/ReturnTrue
function id(x) {
return x;
}
id(!0);
还有!!1
function reflexive(x) {
return x != x;
}
reflexive(NaN);
NaN 属性是代表非数字值的特殊值。该属性用于指示某个值不是数字。NaN 与所有值都不相等,包括它自己。
参考:
http://www.w3school.com.cn/jsref/jsref_nan_number.asp
当然了0/0
也是可以的,因为0/0
返回的是NaN
,然后查了一下,发现思路还真广阔
1.0/0-1.0/0
,Infinity - Infinity
,[] - {}
,function() {return ++window.x;}()
function transitive(x,y,z) {
return x && x== y && y == z && x != z;
}
transitive([],0,[]);
其实这就是关于==
的比较,==
首先会将不同类型进行转换,例如[] == 0
或0 == []
中会先将[]
转化成字符类型,由于为空,所以比较0
就相同
function counter(f) {
var a = f(), b = f();
return a() ===1 && a() == 2 && a() == 3
&& b()=== 1 && b() == 2;
}
counter((x=1)=>_=>x++);
x=>_=>x=-~x
这个题似懂非懂的,欢迎交流~
=>是es6语法中的arrow function
(x) => x + 6
相当于
function(x){
return x + 6;
};
首先这个题原意是这样的,必须让数值能够取1,2,3或1,2,复杂的写法是如下:
counter((x = 0, function(){
return function(){
return (x++) + 1;
};
}))
由于允许Arrow functions
方法,这样的话精简一下就是
counter((x=0,_=>_=>(x++)+1))
(x=1)=>_=>x++
然后在精简一点,因为-~x
表示增加1,~-x
表示减小1
x=>_=>x=-~x
好吧,我还是不理解=>
的运算
https://www.reddit.com/r/programming/comments/4wd32v/return_true_to_win/d673x90/
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
function peano(x) {
return (x++ !== x) && (x++ === x);
}
peano(2**53-1);
因为js中数字类型的范围是-2^53~2^53
,因此当2**53-1 (9007199254740991)
,执行左边时,执行的却是2**53 !== 2**53-1
,然后x
变为2**53
,因为范围是2**53
,所以x++
值不变,等式变为2**53 === 2**53
https://www.zhihu.com/question/29010688
9**17-2也可以
function array(x,y) {
return Array.isArray(x) && !(x instanceof Array) &&
!Array.isArray(y) && (y instanceof Array);
}
array(Array.prototype,{__proto__:Array.prototype});
首先看一下Array.isArray()
函数,其实就是判断是否是数组
Array.isArray([1, 2, 3]); // true
Array.isArray({foo: 123}); // false
Array.isArray('foobar'); // false
Array.isArray(undefined); // false
// all following calls return true
Array.isArray([]);
Array.isArray([1]);
Array.isArray(new Array());
// Little known fact: Array.prototype itself is an array:
Array.isArray(Array.prototype);
// all following calls return false
Array.isArray();
Array.isArray({});
Array.isArray(null);
Array.isArray(undefined);
Array.isArray(17);
Array.isArray('Array');
Array.isArray(true);
Array.isArray(false);
Array.isArray({__proto__:Array.prototype});
接下来稍微介绍一下instanceof
,最简单的意思就是判断类型是否相等,因为instanceof
将只检查原型链接。 它不在乎怎么了什么为什么。 如果对象相同,则返回true,否则返回false。
https://www.ibm.com/developerworks/cn/web/1306_jiangjj_jsinstanceof/
当然了,这个还是我测试出的一个艰难的两个不相等的。。。欢迎交流
看大神的,又有好多,但是不太懂
一开始
{}, Object.create(Array.prototype), Array.isArray=_=>x--, x=1
然后
Array.prototype,Object.create([])
然后
[].__proto__,{__proto__:Array.prototype}
然后
[].__proto__,{__proto__:[]}
然后
0,[Array.isArray=x=>!x]
然后还有另外一种
[][r='__proto__'],y={},y[r]=[]
好吧,最后几种完全不懂
function instance(x,y) {
return x instanceof y && y instanceof x && x !== y;
}
instance(Function,Object);
function instance2(a,b,c) {
return a !== b && b !== c && a !== c
&& a instanceof b
&& b instanceof c
&& c instanceof a;
}
function proto1(x) {
return x && !("__proto__" in x);
}
proto1({__proto__:null});
定义为null
,其实是存在的,而不是undefined
,所以前半部分是true,但是,由于定义的是null
,也就是意味着没有原型,所以后半部分是不存在的
http://www.cnblogs.com/libin-1/p/6014925.html
https://www.zhihu.com/question/60447787/answer/177147536
http://www.cnblogs.com/ziyunfei/archive/2012/10/22/2733748.html
还有一种
Object.create(null)
function undef(x) {
return !{ undefined: { undefined: 1 } }[typeof x][x];
}
undef(document.all);
想了好久,什么是未定义的,但不是未定义的?
网页一般做if (document.all) ...
,并且 Opera将会被认为是IE,因此他们为了解决这个便把document.all
视为未定义的,但却不等于未定义,类似一种可调用的,一种dom数组
大神又给出另外几种答案
undef(1, Object.prototype.number = {})
// or the more succint
undef(1, {}.__proto__.number = {})
function symmetric(x,y) {
return x == y && y != x;
}
symetric(x=0,{valueOf:_=>x++});
如果我们在弱比较中使用具有对象的原语,则对象首先将转换为基本类型。 在这种情况下,我们传递1作为第二个参数,所以这个比较会将一个对象转换为一个数字。
当对象被转换为原语时,有两种检查方法。 哪种方法完全依赖于当目标原语在转换时,大部分时间是字符串。 对于一个字符串,调用的方法是toString。 对于数字,方法是valueOf。
我们传递一个具有自定义valueOf的对象,使得结果永远不会相同。 既然没有参数,我们需要使用全局来切换。 幸运的是,我们需要传递第二个值,无论如何,我们可以使用该字段并同时初始化一个。
这里y
将会得到0
,而此时比较会相同,而右边时y=0,x=1
并不相同。
function ouroborobj(x) {
return x in x;
}
ouroborobj([0]);
格式:(变量 in 对象)……注意
- 当“对象”为数组时,“变量”指的是数组的“索引”;
- 当“对象”为对象是,“变量”指的是对象的“属性”。
所以也就变成了0 in [0]
参考:
http://sunct.iteye.com/blog/1709017
function truth(x) {
return x.valueOf() && !x;
}
truth(d=document.all,d.valueOf=_=>1);
明显document.all
是假,然后满足第二个条件后在利用valueOf进行值覆盖,完成真的转变
当然dalao也有方法,首先''
是无,满足第二个条件,然后利用
'',''.__proto__.valueOf=[]
还有这种,一开始是0,满足第二条件,然后赋值为1,满足第一条件
truth(0,(0).__proto__.valueOf=_=>1)
当然稍微精简一下,由于js语法,要么用()
,要么用..
来表示,也就是说45232..toString(2).length]
就相当于(45232).toString(2).length
truth(0,0..__proto__.valueOf=_=>1)
当然了,有一种答案不是很懂,欢迎交流
(String.prototype.valueOf=_=>true,'')
function wat(x) {
return x('hello') == 'world:)' && !x;
}
wat(d=document.all,d(0).id='hello',d(0).toString=_=>'world:)');
它是未定义的,它是一个像getElementById和getElementByName一样的函数,它是一个有着所有元素的dom集合(类似数组的,也可以通过索引的函数调用)。 因此,我们需要将某个元素的id或name设置为hello
,并明确其toString
方法以返回字符串world :)
.
wat(document.all,document.body.id='hello',document.body.toString=_=>'world:)')
wat((d=document,h=d.body,h.id='hello',h.toString=_=>'world:)',d.all))
wat((d=document).all,h=d.body,h.id='hello',h.toString=_=>'world:)')
wat((d=document).all,d.body.id='hello',hello.toString=_=>'world:)')
当然了,以下ladao给的,但并不能完全能够理解
这将以document.all
为主要。 它也能够获取html元素。 它设置id和toString,以便它通过测试。
wat(d=document.all,d(0).id='hello',d(0).toString=_=>'world:)')
这个利用解构,可以用作表达式并返回原始值,这个技巧还将document.all的第一个元素(我们学到的是html元素)分配给一个临时变量。
wat([y]=document.all,y.id='hello',y.valueOf=_=>'world:)')
var eval = window.eval;
function evil1(x) {
return eval(x+'(x)') && !eval(x)(x);
}
evil1(_=>0);
首先是进行字符串化,使用箭头函数的字符串返回自己的字符串,所以第一个就变成了_=>0(x)
,完全成立,而第二部分则是eval('_=>0')(x)
,当然输出0,最后的(x)
只要()
存在即可,无论其中值如何
var eval = window.eval;
function evil2(x) {
return eval('('+x+')(x)') && !eval(x)(x);
}
evil2(_=>x=_=>0);
var eval = window.eval;
function evil3(parameter) {
return eval('('+parameter+')(parameter)') &&
!eval(parameter)(parameter);
}
function random1(x) {
return Math.random() in x;
}
random1([Math.random=_=>0]);
var rand = Math.random();
function random2(x) {
return rand in x;
}
random2(new Proxy({},{has:_=>1}));
var key = crypto.getRandomValues(new Uint32Array(4));
function random3(x) {
var d = 0;
for (var i=0; ireturn d === 0;
}
random3(Uint32Array.prototype.__proto__={});
还有一种长的
random3(Object.defineProperty(Uint32Array.prototype, 'length', {value: 0}))
var rand = Math.random();
function random4(x) {
return rand === x;
}
不知道,连答案也没找到
function total(x) {
return (x < x) && (x == x) && (x > x);
}
total({valueOf:_=>n--%2},n=2);
// submitted by azzola
const secrets = new Uint32Array(2);
crypto.getRandomValues(secrets);
const [key, value] = secrets;
const vault = {
[key]: value
};
function json(x, y) {
Object.defineProperty(vault, x, { value: y });
const secure = JSON.stringify(Object.freeze(vault));
let copy;
try {
copy = eval(`(${secure})`);
} catch (e) {
// Try again...
copy = JSON.parse(secure);
return key in copy && copy[key] !== vault[key];
}
return void vault;
}
https://www.zhihu.com/question/29010688
https://news.ycombinator.com/item?id=12274697
https://www.reddit.com/r/programming/comments/4wd32v/return_true_to_win/d673x90/
https://qfox.nl/weblog/378