zoukankan      html  css  js  c++  java
  • 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 );
  • 相关阅读:
    Oracle 删除某个用户下的对象
    解决11g导出时,空表不能导出问题
    Oracle常用sql
    Oracle 创建表空间和用户
    chapter4.1、函数,参数
    chapter3.6、标准库datetime
    chapter3.5内建函数
    简单选择排序和二元选择排序
    chapter3.4解析式、生成器
    列表解析练习
  • 原文地址:https://www.cnblogs.com/guozhe/p/15475648.html
Copyright © 2011-2022 走看看