当前位置:网站首页>js傳參時傳入 string有數據;傳入 number時沒有數據;2[0]是對的!number類型數據可以取下標
js傳參時傳入 string有數據;傳入 number時沒有數據;2[0]是對的!number類型數據可以取下標
2022-07-24 02:33: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.不寫不知道,一寫嚇一跳。動動手,說不定有新發現!
/*
希望對你有幫助!
如有錯誤,歡迎指正,非常感謝!
*/
边栏推荐
- 通用机环境下安全版单机数据库使用非root用户管理的解决方案
- 【补题日记】[2022牛客暑期多校2]K-Link with Bracket Sequence I
- Use the pagoda panel to plan tasks and automatically push the website to Baidu for inclusion
- JDBC tool class
- [jailhouse article] virtualization over multiprocessor system on chip an enabling paradigm for
- Beansearcher receives array parameters and logical deletion
- Mysql数据库,分组函数篇
- POP3客户端代码的实现
- BPG notes (III)
- Pyg uses messagepassing to build GCN to realize node classification
猜你喜欢
![[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

JDBC tool class

How to do a good job of accompanying translation

This article shows you how to use SQL to process weekly report data

认识传输层协议—TCP/UDP

Resumption: a deck of cards (54), three people fighting the landlord, what is the probability that the big and small kings are in the same family

The combination sum of C language power deduction question 39. Backtracking method and traversal method

关于缺少编程基础的朋友想转行 ABAP 开发岗提出的一些咨询问题和解答

NetApp FAS系列一个CIFS bug引起的控制器重启案例分享

Discussion on sending redundant API requests for Spartacus UI transfer state of SAP e-commerce cloud
随机推荐
关于 SAP Fiori 应用的离线使用
7 issues to consider before website construction
餐饮连锁门店重塑增长背后的数字化转型
Give me five minutes, give you a "cloud"
How to do a good job of accompanying translation
2022.7.22 JS entry common data types and methods
This article shows you how to use SQL to process weekly report data
【补题日记】[2022牛客暑期多校1]J-Serval and Essay
MySQL---four JDBC
Mysql数据库,排序与单行处理函数篇
因果学习开源项目:从预测到决策!
记于2022.7.21
Jina AI 联合Datawhale,发起学习项目!
JpaRepository扩展接口
网络协议详解 :UDP
[leetcode] sword finger offer 61. shunzi in playing cards
Mysql database, query
【FPGA教程案例39】通信案例9——基于FPGA的交织-解交织数据传输
Rylstim Screen Recorder
[untitled]