Home > Web Front-end > JS Tutorial > body text

Interview question: How to make 'a==1&&a==2&&a==3' true?

青灯夜游
Release: 2023-03-13 20:06:48
forward
1439 people have browsed it

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!

Interview question: How to make 'a==1&&a==2&&a==3' true?

1. Problem analysis

if (a == 1 && a == 2 && a == 3) {
  console.log('Win')
}
Copy after login

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?

Interview question: How to make 'a==1&&a==2&&a==3' true?

#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.

Interview question: How to make 'a==1&&a==2&&a==3' true?

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.

2. Solution

2.1 toString

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
  }
}
Copy after login

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. ifThe whole package triggers the conversion rule of Boolean(), == and the string triggers toString()## on the right side # conversion rules.

Back to this question, the right side of

== 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.

2.2 defineProperties

Data interception method using objects:

Object.defineProperties(window, {
  _a: {
    value: 0,
    writable: true
  },
  a: {
    get: function() {
      return  ++_a
    }
  }
})
Copy after login
Involving object accessor related content

I don’t know if it reminds you of the watch or computed instructions in Vue. ?

3. Summary

Whether you can do this interview question has no meaning. But it is very interesting to understand the knowledge points contained in this interview question.

From implicit type conversion to prototype and prototype chain, and finally to the access rights attribute of the object. If you want to continue to extend, the implementation principle of Vue's two-way binding, the implementation of static in class, etc.

These are why it is necessary to build a front-end knowledge system that can extend from one knowledge point to other related knowledge points.

[Recommended learning:

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!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!