当前位置:网站首页>Problems in the deconstruction and assignment of objects, comparison between empty strings and undefined
Problems in the deconstruction and assignment of objects, comparison between empty strings and undefined
2022-06-13 08:36:00 【webQiang99】
Today, I changed the style of the project , Because the style is manually encapsulated by your tutor , So pass different parameters , There will be different effects , Record a problem encountered today
This is a problematic style 
This is the original style 
This is the code of the original style 

This is the code encapsulated in the component , Think of the parameters passed into the component cType by ’‘ An empty string , according to es6 Object's deconstruction assignment ,cType It should be the default default Well , Later there was a problem with the style , I debugged the breakpoint , Find out cType The value of is ’’, So I went to teacher Ruan Yifeng es6 The official website of the blog reviews the default value of object deconstruction assignment , The original is written like this ,

Only with undefined When strictly equal , Will cause the default value to take effect ,null No way. , An empty string ’' Not good either. , I always thought ‘’ and undefined They are equal. , because ‘’ The comparison will be escaped to false,undefined It will also escape to false( This view is not correct ), But by demo Find out , That's not the truth ,
This is the result of printing 
I rely on ,undefined == false was false, I always thought it was true, Because in the previous impression undefined, and unll Can be converted to false, however undefied and null Judgment is not equal to false It's all the time false,
In order to make yourself understand why it is not equal, go to the relevant articles , Finally, I found this article , It makes me feel better ,JavaScript:undefined!=false The solution And == The rules of comparison
Roughly speaking :undefined It's not equal to false The reason is simple , Because the basic data type is for == When comparing congruently , The rule is to convert the values on both sides into numbers , Let's see if the conversion result numbers are equal ,undefined Into numbers is NAN, So will Number,String,Boolean And undefined When comparing , Always returns false, as for null It's not equal to false Why , Namely null The type of is an object , When the object performs a congruent comparison, it will call valueOf Methods , Returns a scalar , If valueOf Method of does not return a scalar , Will call toString Method , Return scalar ( As for how to call it, please refer to the above article , Very detailed ), Then convert scalars to numbers for comparison , and null There is no valueOf and toString Method , So it's the return false
Now that we know the problem , That would be a good solution , Because it must be strictly equal to undefined The default value of object deconstruction takes effect , So change the empty string to undefined That's all right. .
边栏推荐
- JS gets the first month of the year, the second month to the last month, and the first day to the last day
- Determination of ranking
- How to efficiently manage commodities and inventory in the beverage wholesale industry
- Notes on development experience: TP5 exp query, SQL analysis, JQ, applet, right-click menu, Linux skills, shell skills, mysql, etc
- About redis encapsulation tool class using distributed locks
- 3、 JS notes
- Use of addeventlistener in JS
- 抖音关键词搜索列表接口,超详细的接口对接步骤
- Daffodil upgrade (self idempotent)
- Logstash configuration reference article
猜你喜欢

Buuctf web (III)

Wechat upload picture material interface

Methods of importing and exporting settings in Altium Designer

How about a well-known food material distribution information management system?

HCIP_ MGRE experiment

Microservice project construction III: automatic code generation

Start from scratch - implement the jpetstore website -1- establish the project framework and project introduction

Vs installation of vassistx plug-in causes Chinese input of wpf-xaml file to be garbled. Solution

MySQL installation and configuration under Windows

How can the small and medium-sized lighting industry make use of the digital transformation to stand out from the encirclement?
随机推荐
Young's matrix to find whether a number exists
Request alarm: refer policy: strict origin when cross origin or reference site policy: no refer when downgrade
How to use annotations in word
[virt-manager]远程管理启动虚拟机时,鼠标飘逸的问题
Microservice system architecture construction I: Environment Construction
HCIP_ MGRE experiment
DIY无人机(匿名拓控者P2+F330机架)
regular expression
MySQL installation and configuration under Windows
Custom exception class myexception
容器概念和云原生
从零开始-实现JpetStore网站-1-建立项目框架以及项目介绍
redis. exceptions. ConnectionError: Error 111 connecting to 172.16.8.128:6379. Connection refused.
Tmall product details interface, tmall product coupon interface, tmall API interface, tmall price monitoring interface, tmall price comparison interface, brand rights protection interface, tmall sales
【leetcode周赛记录】第80场双周赛记录
0.一些自己初學Solidworks的疑惑
天猫商品详情接口,天猫商品优惠券接口,天猫api接口,天猫价格监控接口,天猫比价接口,品牌维权接口,天猫销量api接口,接口代码可对接数据分析业务,品牌维权,比价业务,行业分析业务接口代码分享
Notes on MySQL transaction not automatically submitting
情绪处理技巧
Container concept and cloud native