This article will share with you a classic interview question to see how to make "a==1&&a==2&&a==3" true? Through this interview question, I learned the knowledge points contained in it. I hope it will be helpful to everyone!
if (a == 1 && a == 2 && a == 3) { console.log('Win') }
How to make the code in if execute and successfully print out on the consoleWin
?
When I first saw the question, I was blinded. How could such a contradictory situation happen? It is equivalent to how a person can be a child and an adult at the same time. Or the elderly?
#Calm down and find some clues.
It doesn’t say let a be equal to 1 2 3 at the same time.
And js runs in a single thread. Even if they are written on one line, they are executed from left to right. So they are not from the same period in time and space.
Since they are not from the same period, then of course a person can become a child, then an adult, and then an elderly person.
Back to the question, if I want this condition to be true, I need to obtain a once and let it increase by 1 at the same time.
The first method is to use [implicit conversion in the judgment process ]toString
method. I elaborated in my other articleWhy [] == ![] results in true?.
const a = { _a: 0, toString: function() { return ++a._a } }
Run it once, add 1 to _a, and then return.
Because toString is the default method above Object.prototype
, so this method is equivalent to changing the normal The toString
method in the implicit conversion is intercepted.
Knowledge points involving prototypes and prototype chains
The problem can be solved.
Some friends in the comment area said that setting a = true can also solve the problem. Very misleading indeed. In fact, it confuses the priority of implicit conversion. Simply put, implicit conversion consists of two parts: Conversion rules and conditions that trigger conversion. if
The whole package triggers the conversion rule of Boolean()
, ==
and the string triggers toString()## on the right side # conversion rules.
== is a number, and the JS running line is from left to right. Therefore, what is triggered at this time is the
Number() rule. After converting the
true on the left into
1, the
types on both sides are the same After that, naturally no more rules will be triggered, and it no longer constitutes an implicit conversion. So 1 == 1 && 1 == 2 && 1 == 3 is not true.
I remember it now, and it will be better than memorizing it during the interview.Now I will simply modify the question, changing double equals into
three What to do when ?
Everyone knows that=== determines the type first and then the value.
toString here has converted the object into a string by default. If
toStirng is used, the result will not be true.
Object.defineProperties(window, { _a: { value: 0, writable: true }, a: { get: function() { return ++_a } } })
I don’t know if it reminds you of the watch or computed instructions in Vue. ?
javascript advanced tutorial]
The above is the detailed content of Interview question: How to make 'a==1&&a==2&&a==3' true?. For more information, please follow other related articles on the PHP Chinese website!