当前位置:网站首页>js传参时传入 string有数据;传入 number时没有数据;2[0]是对的!number类型数据可以取下标
js传参时传入 string有数据;传入 number时没有数据;2[0]是对的!number类型数据可以取下标
2022-07-24 02:32:00 【琹箐】
目录
一、问题
1.今天真的有被震惊到,js number类型的数据 竟然能够取下标,返回值为 undefined。讲真的我一直以为这个会报错的。没想到竟然没有问题,实测如下:
1)代码
//测试代码
let obj={type:2} //undefined
console.log(obj["type"]);//2
console.log(obj["type"][0]);//undefined
console.log(2[0]);//undefined2)结果

2.同事有封装一个组件,但是传参的时候非常奇怪,传入的是字符串的时候 接口请求头就 有数据;但是转换为 number类型后,请求头就没有数据了。百思不得其解。
仔细看了一下代码发现了这样一段代码:我天哪,2[0]什么操作,竟然没有报错,去控制台打印了一下确实没有问题(惊悚.jpg)
分析:
1)传入filterData={type: 2}:当type为 number类型的 2时,因为filters[key][0]为 undefined,所以最终导致 this.filterData["type"]为undefined;
2)传入filterData={type: ‘2’}:当type为 string类型的时候, "2"[0] 为 2,所以 this.filterData["type"]为 2

3.其他类型的数据是否也有这种情况?
常识告诉我,只有字符串和数组可以这样子取下标呀。难道其他基本数据类型都可以这样取,只不过取不到值而已?经测试,不全是如此。
null和undefined不可以用下标访问,直接报错;
object,boolean,number,symbol 可以使用下标访问,但是取到的值均为undefined;
string,array可以使用下标访问,能正常取到值;
1)测试结果

二、解决方法
上面说了半天,问题还是在呀,怎么样才可以传入的是number,还让 this.filterData["type"]的值是正确的呢?
开动你的小脑筋,要让 filters["type"][0] 为 一个数字,比如是2,那么要能用下标取到正常值,现在有两种方式,要么 filters["type"]是一个 string,要么是一个数组 array。我们想要传一个number类型的,肯定不能是string,所以filters["type"]是一个 array,里面装着一个 number类型的数据。传入filterData={type: [2]},问题完美解决!!!
三、总结
1.除了null和undefined取下标会报错,其余基本数据类型都不会报错,但是这些数据类型中只有array和string能够正常取到数据,其余取到的都是 undefined.
2.虽然是个小问题,但是我感觉自己从根本认知上就存在问题。还是记录一下吧!
3.不写不知道,一写吓一跳。动动手,说不定有新发现!
/*
希望对你有帮助!
如有错误,欢迎指正,非常感谢!
*/
边栏推荐
- 氢能创业大赛 | 国华投资董事长刘小奇:发挥风光氢储融一体化优势 高水平承办创业大赛
- [datasets] - downloading some datasets of flyingthings3d optical flow
- [FPGA tutorial case 39] communication case 9 - interleaving deinterleaving data transmission based on FPGA
- 关于 SAP 电商云 Spartacus UI Transfer State 冗余 API 请求发送的讨论
- Hydrogen entrepreneurship competition | Liu Xiaoqi, chairman of Guohua Investment: take advantage of the integration of scenery, hydrogen storage and finance to host the entrepreneurship competition a
- [jailhouse article] virtualization over multiprocessor system on chip an enabling paradigm for
- Implementation of POP3 client code
- MySQL---four JDBC
- 中城院真的在帮助供应商解决问题吗?
- [untitled]
猜你喜欢

Writing of graph nodes that trigger different special effects during the day and at night in Tiktok

How to do a good job of accompanying translation
![【补题日记】[2022牛客暑期多校1]C-Grab the Seat](/img/86/1cf3bbc53d9365bb95dae6d532e276.png)
【补题日记】[2022牛客暑期多校1]C-Grab the Seat

認識傳輸層協議—TCP/UDP

WordPress website SEO complete tutorial

Give me five minutes, give you a "cloud"

Causal learning open source project: from prediction to decision!

Responsive layout a web page displays different effects on different devices) meta:vp

Tutoriel sur l'utilisation de la ligne de temps unitaire
![[diary of supplementary questions] [2022 Niuke summer school 1] c-grab the seat](/img/86/1cf3bbc53d9365bb95dae6d532e276.png)
[diary of supplementary questions] [2022 Niuke summer school 1] c-grab the seat
随机推荐
Redraw the button and make your own circular LED indicator
1000个Okaleido Tiger首发上线Binance NFT,引发抢购热潮
QT display Chinese garbled code
Wallys/DR4019S/IPQ4019/11ABGN/802.11AC/high power
Mysql数据库,查询篇
【补题日记】[2022牛客暑期多校1]D-Mocha and Railgun
网络协议详解:TCP Part1
【FPGA教程案例39】通信案例9——基于FPGA的交织-解交织数据传输
Installation, configuration and use of sentry
Combined with actual combat, analyze gb/t 28181 (II) -- equipment directory synchronization
我国科学家在高安全量子密钥分发网络方面取得新进展
Seatunnel architecture
Network protocol details: UDP
Digital transformation behind the reshaping growth of catering chain stores
22 -- range and of binary search tree
Qml- use listview to build a three-level treeview architecture
关于 SAP Fiori 应用的离线使用
Go基础笔记_5_数组切片
ACM SIGIR 2022 | interpretation of selected papers of meituan technical team
Writing of graph nodes that trigger different special effects during the day and at night in Tiktok