We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
如何让(a==1 && a==2 && a==3)的值为true?改为===呢?
===
The text was updated successfully, but these errors were encountered:
let a = { value: 1, toString() { return a.value++; } } console.log(a == 1 && a == 2 && a == 3);
PS: 这里利用了 对象{}的原生属性 toString, 他会在对象运行时先执行,还可以使用valueOf,valueOf比toString先运行。
var value = 0; Object.defineProperty(window, 'a', { get: function() { return this.value += 1; } }); console.log(a === 1 && a === 2 && a === 3)
var aᅠ = 1; var a = 2; var ᅠa = 3; console.log(aᅠ === 1 && a === 2 && ᅠa=== 3);
Sorry, something went wrong.
var a = { value: 0 }; a.valueOf = function (){ return this.value += 1 } console.log((a==1 && a==2 && a==3));
1、如果类型为基本类型,就返回这个值; 2、如果类型为对象,就返回对象的valueOf(),如果返回类型为基本类型,就返回这个值; 3、以上均未通过,则执行值的toString(); 4、还不行,就抛出类型错误;
var value = 0; Object.defineProperty(window, 'a', { get: function () { return this.value += 1 } }) console.log(a===1 && a===2 && a===3);
当当当,主要原因是Proprety(属性描述符)函数
属性描述符函数,有两个方法,get 和 set,通过get或者set,可以强制键值,设定可选键值,正常对象通过Proprety设置的强制键值,可以用Object. 来使用,因此,以上方法先设置一个可以拿到的值,然后比对,就实现了题目需求(注:由于题目是直接 a === b, 因此,首先将a挂载到了windows上)。
No branches or pull requests
The text was updated successfully, but these errors were encountered: