Q3.看看 toString() 和 valueOf() 到底怎么回事?

本文深入探讨JavaScript中不同数据类型间的转换与比较规则,包括ToNumber、ToPrimitive操作及对象与原始类型之间的转换过程。重点讲解valueOf和toString方法的作用与调用优先级,以及这些方法如何影响比较结果。

前奏 看看 ==

N表示ToNumber操作,即将操作数转为数字。

P表示ToPrimitive操作,即将操作数转为原始类型的值(Undefined、Null、Boolean、Number和String等五种)

preview

  • undefined == null,结果是true。且它俩与所有其他值比较的结果都是false

  • String == Boolean,需要两个操作数同时转为Number。

  • String/Boolean == Number,需要String/Boolean转为Number。

  • Object == Primitive,需要Object转为Primitive(具体通过valueOftoString方法)。

重点 看看 valueof() 和 toString()

      所有JS数据类型都拥有valueOf和toString这两个方法,null和 undefined 除外。它们继承自Object(即 Object.prototype.valueOf()和 Object.prototype.toString()),当然也可能被子类重写。

    是否重写 toString() 和 valueOf()  和  其返回值是否为原始数据类型,影响比较结果

1. 都重写时: 有操作符 且 valueOf()返回值是非原始类型 且 console.log( a == b) ,b为非原始类型时,两个方法都不调其实。

2.都重写时: 有操作符 且 valueOf()返回值是原始类型   :valueOf()优先级高于toString()

3. 都重写时: 有操作符 且 valueOf()返回值是非原始类型   :两个方法都调(先调用 valueOf() 再 toString() ) 给人一种假象 toString()优先级高于valueOf()

4. 只重写了 toString() :  toString()优先级高于valueOf()

5.只重写了valueOf() :  valueOf()优先级高于toString()

6.都不重写 :默认调用的valueof() :可以通过== 和 === 比较看。

7. console 中打印展示 有可能 不调用 valueOf  而并非  [1,2] 和 [1,2] 为两块地址而显示false 的理解。

拓展:toString()可以判断数据类型

 

遗留的问题:图上的报错说对象不能转化成基本类型 为什么?希望大家能帮我解答哈

请分析下面代码,指出Step Form1 2 3对应的Qtime相同,是会都显示吗/* */ package com.smics.apps.erc.action.user; /* */ /* */ import com.smics.apps.erc.action.util.FormatUtil; /* */ import com.smics.apps.erc.domain.ErcMastForm; /* */ import com.smics.apps.erc.domain.GroupResult; /* */ import com.smics.apps.erc.domain.QTimeStr; /* */ import com.smics.apps.erc.domain.SolutionStepQtime; /* */ import com.smics.apps.erc.domain.StepForm; /* */ import java.util.ArrayList; /* */ import java.util.List; /* */ /* */ public class ComeinMainFlowQTimeAction extends BaseQTimeAction /* */ { /* */ private List seqs; /* */ private String method; /* */ private List qtimeStrs; /* */ private List stepQtimes; /* */ /* */ public String execute() /* */ throws Exception /* */ { /* 33 */ this.seqs = FormatUtil.splitStrBySeparator(this.groupResult.getSteps(), ","); /* 34 */ this.qtimeStrs = this.requestForm.getQtimeStrs(); /* */ /* 37 */ if ((this.seqs == null) || (this.seqs.size() == 0)) { /* 38 */ return "success"; /* */ } /* */ /* 42 */ List tStepQtimes = new ArrayList(); /* */ /* 44 */ List tStepForms = this.requestForm.getStepForms(); /* */ /* 46 */ for (int i = 0; i < this.seqs.size(); i++) { /* 47 */ String sSRCStepSeq = (String)this.seqs.get(i); /* */ /* 49 */ for (int j = 0; j < tStepForms.size(); j++) { /* 50 */ StepForm tStepForm = (StepForm)tStepForms.get(j); /* */ /* 52 */ int iItem = tStepForm.getStepSerialNo(); /* 53 */ String iItemStr = Integer.valueOf(iItem).toString(); /* 54 */ if (iItem < 10) iItemStr = "0" + iItemStr; /* */ /* 57 */ List peSolutionQtimes = tStepForm.getStepQtimes(); /* */ /* 59 */ if ((peSolutionQtimes == null) && (peSolutionQtimes.size() <= 0)) /* */ continue; /* 61 */ if (sSRCStepSeq.equalsIgnoreCase(iItemStr)) { /* 62 */ for (int m = 0; m < peSolutionQtimes.size(); m++) /* */ { /* 64 */ SolutionStepQtime peSolutionQtime = (SolutionStepQtime)peSolutionQtimes.get(m); /* */ /* 66 */ QTimeStr tStepQTime = new QTimeStr(); /* */ /* 68 */ tStepQTime.setPlanId(peSolutionQtime.getPlanId()); /* 69 */ tStepQTime.setPlanVer(peSolutionQtime.getPlanVer()); /* 70 */ tStepQTime.setMpsStepSeq(peSolutionQtime.getMpsStepSeq()); /* */ /* 72 */ tStepQTime.setControlType(peSolutionQtime.getControlType()); /* 73 */ tStepQTime.setEndStepSeq(peSolutionQtime.getEndStepSeq()); /* 74 */ tStepQTime.setExpireAction(peSolutionQtime.getExpireAction()); /* 75 */ tStepQTime.setIntervalTime(peSolutionQtime.getIntervalTime()); /* 76 */ tStepQTime.setTmpStartStepSeq(peSolutionQtime.getStrStepSeq()); /* */ /* 79 */ tStepQTime.setStrStepSeq(sSRCStepSeq); /* 80 */ tStepQtimes.add(tStepQTime); /* */ } /* */ /* 83 */ break; /* */ } /* */ } /* */ /* */ } /* */ /* 89 */ this.stepQtimes = tStepQtimes; /* 90 */ return "success"; /* */ } /* */ /* */ public List getSeqs() /* */ { /* 101 */ return this.seqs; /* */ } /* */ /* */ public void setSeqs(List seqs) /* */ { /* 106 */ this.seqs = seqs; /* */ } /* */ /* */ public String getMethod() /* */ { /* 111 */ return this.method; /* */ } /* */ /* */ public void setMethod(String method) /* */ { /* 116 */ this.method = method; /* */ } /* */ /* */ public List getQtimeStrs() /* */ { /* 121 */ return this.qtimeStrs; /* */ } /* */ /* */ public void setQtimeStrs(List qtimeStrs) /* */ { /* 126 */ this.qtimeStrs = qtimeStrs; /* */ } /* */ /* */ public List getStepQtimes() /* */ { /* 137 */ return this.stepQtimes; /* */ } /* */ /* */ public void setStepQtimes(List stepQtimes) /* */ { /* 148 */ this.stepQtimes = stepQtimes; /* */ } /* */ } /* Location: C:\Users\JE03789\Documents\erc.zip * Qualified Name: erc.WEB-INF.classes.com.smics.apps.erc.action.user.ComeinMainFlowQTimeAction * JD-Core Version: 0.6.0 */
最新发布
12-20
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值