Object.defineProperties的使用

76 阅读1分钟

a === 1 && a === 2 && a === 3 成立

Object.defineProperties(window, {
  _a: {
    value: 0,
    writable: true
  },
  a: {
    get: function() {
      return  ++_a
    }
  }
});
if (window.a === 1 && window.a === 2 && window.a === 3) {
    console.log('true');
};

a == 1 && a == 2 && a == 3 成立

let a = {
    i: 0,
    valueOf: () => {
        return ++ a.i;
    }
}
        
if (a == '1' && a == '2' && a == '3') {
    console.log('true')
}
let b = {
    _b: 0, toString() {
            return ++b._b;
    }
}
if (b == 1 && b == 2 && b == 3) {
    console.log('true')
}