js-return ture to win

来源:互联网 发布:oracle创建数据库语句 编辑:程序博客网 时间:2024/06/17 09:45

我也有很多不会或是不理解,欢迎一起交流学习

链接:

http://alf.nu/ReturnTrue

id(2 chars)

function id(x) {    return x;}id(!0);

还有!!1

reflexive(3 chars)

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;}()

transitive(7 chars)

function transitive(x,y,z) {    return x &&  x== y && y == z && x != z;}transitive([],0,[]);

其实这就是关于==的比较,==首先会将不同类型进行转换,例如[] == 00 == []中会先将[]转化成字符类型,由于为空,所以比较0就相同

counter(9 chars)

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

peano(5 chars)

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也可以

array(20 chars)

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]);  // trueArray.isArray({foo: 123}); // falseArray.isArray('foobar');   // falseArray.isArray(undefined);  // false
给出一下稍微特殊点的:
// all following calls return trueArray.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 falseArray.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]=[]

好吧,最后几种完全不懂

instance

function instance(x,y) {  return x instanceof y && y instanceof x && x !== y;}instance(Function,Object);

instance2

function instance2(a,b,c) {  return a !== b && b !== c && a !== c      && a instanceof b      && b instanceof c       && c instanceof a;}

proto1(8 chars)

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)

undef(12 chars)

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 succintundef(1, {}.__proto__.number = {})

symmetric(20 chars)

function symmetric(x,y) {    return x == y && y != x;}symetric(x=0,{valueOf:_=>x++});

如果我们在弱比较中使用具有对象的原语,则对象首先将转换为基本类型。 在这种情况下,我们传递1作为第二个参数,所以这个比较会将一个对象转换为一个数字。
当对象被转换为原语时,有两种检查方法。 哪种方法完全依赖于当目标原语在转换时,大部分时间是字符串。 对于一个字符串,调用的方法是toString。 对于数字,方法是valueOf
我们传递一个具有自定义valueOf的对象,使得结果永远不会相同。 既然没有参数,我们需要使用全局来切换。 幸运的是,我们需要传递第二个值,无论如何,我们可以使用该字段并同时初始化一个。
这里y将会得到0,而此时比较会相同,而右边时y=0,x=1并不相同。

ouroborobj(3 chars)

function ouroborobj(x) {    return x in x;}ouroborobj([0]);

格式:(变量 in 对象)……注意
- 当“对象”为数组时,“变量”指的是数组的“索引”;
- 当“对象”为对象是,“变量”指的是对象的“属性”。

所以也就变成了0 in [0]

参考:

http://sunct.iteye.com/blog/1709017

truth(27 chars)

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,'')

wat(51 chars)

function wat(x) {    return x('hello') == 'world:)' && !x;}wat(d=document.all,d(0).id='hello',d(0).toString=_=>'world:)');

它是未定义的,它是一个像getElementById和getElementByName一样的函数,它是一个有着所有元素的dom集合(类似数组的,也可以通过索引的函数调用)。 因此,我们需要将某个元素的idname设置为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:)')

evil1(4 chars)

var eval = window.eval;function evil1(x) {    return eval(x+'(x)') && !eval(x)(x);}evil1(_=>0);

首先是进行字符串化,使用箭头函数的字符串返回自己的字符串,所以第一个就变成了_=>0(x),完全成立,而第二部分则是eval('_=>0')(x),当然输出0,最后的(x)只要()存在即可,无论其中值如何

evil2(8 chars)

var eval = window.eval;function evil2(x) {    return eval('('+x+')(x)') && !eval(x)(x);}evil2(_=>x=_=>0);

evil3

var eval = window.eval;function evil3(parameter) {    return eval('('+parameter+')(parameter)') &&           !eval(parameter)(parameter);}

random1(18 chars)

function random1(x) {    return Math.random() in x;}random1([Math.random=_=>0]);

random2(19 chars)

var rand = Math.random();function random2(x) {  return rand in x;}random2(new Proxy({},{has:_=>1}));

random3(34 chars)

var key = crypto.getRandomValues(new Uint32Array(4));function random3(x) {    var d = 0;    for (var i=0; i<key.length; i++) {        d |= key[i] ^ x[i];    }    return d === 0;}random3(Uint32Array.prototype.__proto__={});

还有一种长的

random3(Object.defineProperty(Uint32Array.prototype, 'length', {value: 0}))

random4

var rand = Math.random();function random4(x) {    return rand === x;}

不知道,连答案也没找到

total(21 chars)

function total(x) {    return (x < x) && (x == x) && (x > x);}total({valueOf:_=>n--%2},n=2);

json

// 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

原创粉丝点击