Skip to content

[js] 第64天 如何让(a==1 && a==2 && a==3)的值为true,把"=="换成"==="后还能为true吗? #295

Open
@haizhilin2013

Description

@haizhilin2013
Collaborator

第64天 如何让(a==1 && a==2 && a==3)的值为true,把"=="换成"==="后还能为true吗?

Activity

xn213

xn213 commented on Jun 18, 2019

@xn213

好像是利用 valueOf() toString() ==的类型转换实现, 换成 === 不能为true

haizhilin2013

haizhilin2013 commented on Jun 18, 2019

@haizhilin2013
CollaboratorAuthor

@xn213 如果为"=="时,那a的值为多少呢?换成"==="后不能为true了吗?你的理由是什么呢?

xn213

xn213 commented on Jun 18, 2019

@xn213

@xn213 如果为"=="时,那a的值为多少呢?换成"==="后不能为true了吗?你的理由是什么呢?

==可以进行类型转换, 才可能等1等2等3 , === 即先比较类型再比较值, 类型不同直接返回false, 理解这样,

或许 nothingIsImpossible,,,,,

haizhilin2013

haizhilin2013 commented on Jun 18, 2019

@haizhilin2013
CollaboratorAuthor

@xn213 如果为"=="时,那a的值为多少呢?换成"==="后不能为true了吗?你的理由是什么呢?

==可以进行类型转换, 才可能等1等2等3 , === 即先比较类型再比较值, 类型不同直接返回false, 理解这样,

或许 nothingIsImpossible,,,,,

但我认为是可以做得到的,期待……

WebMrYang

WebMrYang commented on Jun 19, 2019

@WebMrYang

(a++==1 && a++==2 && a++==3)

jiangli373

jiangli373 commented on Jun 19, 2019

@jiangli373
var val = 0;

Object.defineProperty(global, 'a', {
  get: function(){
    return ++val;
  }
})
WANGSHUZHI

WANGSHUZHI commented on Jun 19, 2019

@WANGSHUZHI

期待这个答案

zhulinghao

zhulinghao commented on Jun 19, 2019

@zhulinghao
let a = {
	value: 0,
	valueOf() {
	    return ++this.value;
	}
}
a == 1 && a == 2 && a == 3 // true
haizhilin2013

haizhilin2013 commented on Jun 19, 2019

@haizhilin2013
CollaboratorAuthor
let a = {
	value: 0,
	valueOf() {
	    return ++this.value;
	}
}
a == 1 && a == 2 && a == 3 // true

改为全等的时候呢?

AnsonZnl

AnsonZnl commented on Jun 19, 2019

@AnsonZnl
Contributor
const a = { value : 0 };
a.valueOf = function() {
    return this.value += 1;
};

console.log(a==1 && a==2 && a==3); //true

简单来说就是==会触发valueOf()方法
参考:https://juejin.im/post/5bfcc632f265da61493353cc

forever-z-133

forever-z-133 commented on Jun 19, 2019

@forever-z-133

还可以利用 Array 与数字判断时会转字符串再转数字的规律,改写 join 方法。

var a = [1, 2, 3];
a.join = function() { return this.shift() };
console.log(a == 1 && a == 2 && a == 3); // true
xn213

xn213 commented on Jun 19, 2019

@xn213

#295 (comment)

那参考这篇文章分析的应该足够了

HuaRongSAO

HuaRongSAO commented on Jun 21, 2019

@HuaRongSAO

=== 严格相等,会比较两个值的类型和值
== 抽象相等,比较时,会先进行类型转换,然后再比较值

"==" 转化规则:
首先通过valueOf 转换,即 obj.valueOf()方法的返回值
如果 obj.valueOf()方法的返回值是原始类型,那么直接返回
如果不是,再通过 obj.toString()方法转换
如果obj.toString()返回的是原始类型,直接返回该值
如果还不是原始类型,抛出不能转换异常。

leehf

leehf commented on Jul 5, 2019

@leehf

var a=[1,2,3];
a.join=a.shift;

console.log(a==1&&a==2&&a==3)

KiraYo4kage

KiraYo4kage commented on Jul 5, 2019

@KiraYo4kage
let val = 0;
Reflect.defineProperty(window, 'a', {
  get: () => ++val,
});
(a===1 && a===2 && a===3)
canvas2000

canvas2000 commented on Jul 18, 2019

@canvas2000

let a
!(a==1 && a==2 && a==3)

wsgwwdaa

wsgwwdaa commented on Aug 30, 2019

@wsgwwdaa

let a
!(a==1 && a==2 && a==3)

你可真是个天才

seho-dev

seho-dev commented on Oct 10, 2019

@seho-dev

let a
!(a==1 && a==2 && a==3)

这个答案太秀了

ducky-YFH

ducky-YFH commented on Oct 26, 2019

@ducky-YFH

let a
!(a==1 && a==2 && a==3)

独秀同学请坐

censek

censek commented on Dec 10, 2019

@censek

还是没有懂……

summarychm

summarychm commented on May 6, 2020

@summarychm

利用Object.defineProperty添加get描述符就可以了吧.

"use strict;";
var arr = [1, 2, 3];
Object.defineProperty(globalThis, "a", {
  get: function () {
    return arr.shift();
  },
});
console.log("结果:", a === 1 && a === 2 && a === 3);
smile-2008

smile-2008 commented on Jan 2, 2021

@smile-2008
let a = {
	value: 0,
	valueOf() {
	    return ++this.value;
	}
}
a == 1 && a == 2 && a == 3 // true
1684838553

1684838553 commented on Dec 9, 2021

@1684838553
  var i = 1
  Number.prototype.valueOf = function(){
      return i++
  }
  var a = new Number(i)
  if(a==1 && a==2 && a==3){
      console.log(true,typeof a)
  }

把“==”换成“===”不能为true
在这里,a的数据类型为object

xiaoqiangz

xiaoqiangz commented on Jun 16, 2022

@xiaoqiangz

const a = { value: 0 }
a.valueOf = function() {
return this.value +=1
}
console.log(a==1 && a==2 && a==3)

var value = 0
Object.defineProperty(window, 'b',{
  get: function() {
    return this.value +=1
  }
} )
console.log(b === 1 && b === 2 && b === 3)

var arr = [1,2,3]
Object.defineProperty(window, 'c', {
  get: function() {
    return arr.shift()
  }
})
console.log(c === 1 && c === 2 && c === 3)
dragon-sing

dragon-sing commented on Nov 1, 2022

@dragon-sing
let returnValue = 0;
Object.defineProperty(globalThis, "a", {
  get: function () {
    return ++returnValue;
  },
});

console.log("结果:", a === 1 && a === 2 && a === 3);
panpanxuebing

panpanxuebing commented on Dec 14, 2024

@panpanxuebing
a = {
    value: 1,
    valueOf() { return this.value++ }
}

引用类型和数字使用==比较时,依次调用其toString和toValue方法。改为全等后不触发隐式转换。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    jsJavaScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @jiangli373@smile-2008@summarychm@haizhilin2013@HuaRongSAO

        Issue actions

          [js] 第64天 如何让(a==1 && a==2 && a==3)的值为true,把"=="换成"==="后还能为true吗? · Issue #295 · haizlin/fe-interview