• js 中怎么使 if(aᅠ==1 && a== 2 && ᅠa==3) 返回 true?


    toString / valueOf : 最直接的方案
    
    let a = {
        i: 1,
        toString: function () {
            return a.i++;
        }
    }
    

      

    getter : 新式
    
    var i = 0;
     
    with({
      get a() {
        return ++i;
      }
    }) {
      if (a == 1 && a == 2 && a == 3)
        console.log("wohoo");
    }
     
    // or
     
    Object.defineProperty(window, 'a', {
        get: function() {
            return ++val;
        }
    });
     
    // or
     
    window.__defineGetter__( 'a', function(){
        if( typeof i !== 'number' ){
            // define i in the global namespace so that it's not lost after this function runs
            i = 0;
        }
        return ++i;
    });
    es6 Proxy
    
    var a = new Proxy({ i: 0 }, {
        get: (target, name) => name === Symbol.toPrimitive ? () => ++target.i : target[name],
    });
    console.log(a == 1 && a == 2 && a == 3);
    别具匠心
    
    // This works because == invokes toString which calls .join for Arrays.
    a = [1,2,3];
    a.join = a.shift;
    console.log(a == 1 && a == 2 && a == 3);
    新潮
    
    // Another solution, using Symbol.toPrimitive which is an ES6 equivalent of toString/valueOf
    let a = {[Symbol.toPrimitive]: ((i) => () => ++i) (0)};
     
    console.log(a == 1 && a == 2 && a == 3);
    覆盖式: 原理同上, if 后面有一个字符. 在 chrome 调试中可以看得到, 你没法覆盖掉真正的 if. 所以大括号必须新起一行
    
    if‌=()=>!0;
     
    var i = 1;
    if‌(i == 1 && i == 2 && i == 3)
    {
        console.log(i)
    }
    拓展: 数字变量名
    
    var  a = 1;
    var ᅠ1 = a;
    var ᅠ2 = a;
    var ᅠ3 = a;
    console.log( a ==ᅠ1 && a ==ᅠ2 && a ==ᅠ3 );
  • 相关阅读:
    Fix “Could not flush the DNS Resolver Cache: Function failed during execution” When Flushing DNS
    Spring Boot 2.0 教程 | AOP 切面统一打印请求日志
    FTP服务器原理
    SAMBA 服务器原理
    时间服务器:NTP 服务器
    账号控管:NIS服务器
    NFS服务器原理
    DHCP服务器原理
    Linux防火墙
    linux网络完全与防护
  • 原文地址:https://www.cnblogs.com/guozhe/p/15475648.html
Copyright © 2020-2023  润新知