-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[js] 第64天 如何让(a==1 && a==2 && a==3)的值为true,把"=="换成"==="后还能为true吗? #295
Comments
好像是利用 |
@xn213 如果为"=="时,那a的值为多少呢?换成"==="后不能为true了吗?你的理由是什么呢? |
或许 nothingIsImpossible,,,,, |
但我认为是可以做得到的,期待…… |
(a++==1 && a++==2 && a++==3) |
|
期待这个答案 |
|
改为全等的时候呢? |
简单来说就是 |
还可以利用 Array 与数字判断时会转字符串再转数字的规律,改写 join 方法。
|
那参考这篇文章分析的应该足够了 |
"==" 转化规则: |
var a=[1,2,3]; console.log(a==1&&a==2&&a==3) |
let val = 0;
Reflect.defineProperty(window, 'a', {
get: () => ++val,
});
(a===1 && a===2 && a===3) |
let a |
你可真是个天才 |
这个答案太秀了 |
独秀同学请坐 |
还是没有懂…… |
利用Object.defineProperty添加get描述符就可以了吧. "use strict;";
var arr = [1, 2, 3];
Object.defineProperty(globalThis, "a", {
get: function () {
return arr.shift();
},
});
console.log("结果:", a === 1 && a === 2 && a === 3); |
|
把“==”换成“===”不能为true |
const a = { value: 0 }
|
let returnValue = 0;
Object.defineProperty(globalThis, "a", {
get: function () {
return ++returnValue;
},
});
console.log("结果:", a === 1 && a === 2 && a === 3); |
a = {
value: 1,
valueOf() { return this.value++ }
} 引用类型和数字使用==比较时,依次调用其toString和toValue方法。改为全等后不触发隐式转换。 |
第64天 如何让(a==1 && a==2 && a==3)的值为true,把"=="换成"==="后还能为true吗?
The text was updated successfully, but these errors were encountered: