• Writing in the front

    What if I told you that a == 1 && a == 2 && a == 3 and the result was true? You might wonder, how can a be all three? ! But it does exist!

    Today, let me unveil this mystery for you!

  • The underlying core

    A == 1 && a == 2 && a == 3

    Let’s look at a piece of code

        const a = {
            value:1
        }
    
        console.log(a == 1)
    Copy the code

    Many people may think this is true at first glance, but I can responsibly tell you that it is false! Here we need to make it clear that value=1 is just a property of A, not a itself. Let’s look at another piece of code:

    const a = {
        value: 1.valueOf() {
            return this.value
        }
    }
    
    console.log(a == 1) //true
    
    Copy the code

    Why did I add a valueOf and this is true? Having said that, it’s worth looking at the core principles behind ==. The conversion of a complex data type to a simple data type takes place in four steps

    Citing theThree yuan blog

    According to what process is the transfer of an object to its original type run? When an object is converted to its original type, it calls the built-in [ToPrimitive] function, for which the logic is as follows: 1. If there is a Symbol. ToPrimitive () method, call it first and return 2. Call valueOf() and return 3 if converted to primitive type. Call toString() and return 4 if converted to primitive type. If neither returns the original type, an error will be reportedCopy the code
  • The implementation process

    So if you want to realize a == 1 && a == 2 && a == 3 at the beginning of the article, we just need to apply it simply and copy one of the methods. The code is as follows:

    var a = {
       // Set the default value to 0
       value: 0.// Copy the valueOf method
       valueOf() {
           // Let the value increase slowly
           this.value++;
           return this.value; }}console.log(a == 1 && a == 2 && a == 3);//true
    Copy the code

  • update

    DefineProperty Object. DefineProperty Object. DefineProperty Object. DefineProperty Object. The official documentation

    Syntax: object.defineProperty (obj, prop, desc)

    Obj: The current object for which attributes need to be defined

    Prop Specifies the name of the property to be defined

    Desc attribute descriptor

    Code implementation

    // Define a temporary variable
    var val = 0;
    
    // Capture a globally and return ++val as the value of a
    Object.defineProperty(window.'a', {
        get: function () {
            return++val; }});console.log(a === 1 && a === 2 && a === 3); // true
    Copy the code

    For defineProperty and proxy I am also learning, hope there is a bull guide!


  • Write in the last

    Here we need to keep in mind what process does the conversion of an object to its original type follow?

    When an object is converted to its original type, it calls the built-in [ToPrimitive] function, for which the logic is as follows:

    1. If there is a Symbol. ToPrimitive () method, call it first and return it

    2. Call valueOf(), which returns if converted to the original type

    3. Call toString(), which returns if converted to the primitive type

    4. If no primitive type is returned, an error will be reported

    I am river, front intern a, welcome to each big guy didi, if the article is not correct please be corrected!