JavaScript 中 Array() 和 new Array() 都可以用来生成数组,但是有区别吗?

Array() 的行为:

Array() // Array []
Array(3) // Array [ <3 个空的存储位置> ]
Array(3, 5) // Array [ 3, 5 ]

new Array() 的行为:

new Array() // Array []
new Array(3) // Array [ <3 个空的存储位置> ]
new Array(3, 5) // Array [ 3, 5 ]

可以看到 Array() 和 new Array() 行为是一致的。

但有什么区别吗?

zhanbao
浏览 2283回答 1
1回答

慕瓜9220888

AFMBKLIFFWIKTSTQGRKBJOAQZGFBSEMVYRHANHWVQJQGPXYNPXGDGZIQLRCSVOBIJOTSFYYLAPNJYKATODUTBDGZZSSMIDSFVBMJUJLTTQPMXYXSHUXGTKSXEQWFLLIIAJNDWTSBLAETWZMVHCWBCFVYBKKTSTWGJCZKIQFSFOIAXCQROXNUVLTVDXQDMGMEWLOHNAKQIPLYXNCRSRTYRAZSXEMYDWGBAYZHWSRWZPLQVHPCVEGQHFEZJSUTCDZRSLKQJTUSEUXRUGBFBCETNDNIQOQNGNTWGUXMOYHUHFWTPUNAQMGXFJYHRAXKLTLUQNZCBJOQSIKNIVQYXFFIMMIQEKFSRYHVSBTRYXKNJJIUJFIYECASASVVIRMAJYTMCFPCJVFOIYOVICHJYNQWCVCOHTBNDQVFXZUOUDDCNMYQWWPINUWONKJNQKLEJOBNHQVSUZLLUXQKBDVKHGLFOZAPBUNTBBTENFVYUESTLDIVVSEPGLOTMVYYWHPUGPWPVBDFUPVFHUAVACUCFEEYMKWEWFSFCNHVUNATNQBMTFBYRAXYGIQSLIRIZOEGZMMCMUMJVIERUXONYMIVLRUTQBTDDHLIJIQFBBTPVKNNPIISDCDVACLNGARSNZMFOVRHVQKTWVYFDZYGIRHKHJXTYAVONUTUCLNFREEDOQDPFOBXXMKJMGGJDRJKTBKKPVOCYNFBZJZCDSUCDGNJMEERXIYVZLCEHISVBXSTBGVPIUWDMWVDADWTOWIACBYOLSTIUMCYYLCTLRMPSCXUYAYCLEHRAMSXMFSVYKUQSTQDZCTHFOZTCKDARDCOHAJGSTSTSFORVAMBGIYBOXRJXJKWMPWYPBTGATMPDRGSHKFVBJTIKMLBXAAASUSSRUOCUMODQZMVQRJSTXQTCPOAFQXEHXELQFRAAMQCDSXMPCIGFWWVNKQGGGQFHGQGGNBSEZRLXHHKLQYXATOEYCSHQHHHOEIBAZWDOOMADDDTUALPHNWXROYFMRNPOFJJARMLUDAQXRRGIBAXHEVEJZZHOPZJCRCUTQUHOECBQTIYPZTRKMSFWTBKPPILBISJETCRAHXYIEKAZBYFWMBPOPYFMDNJHXZSUBYOPKCLRVIFWMUNGYFCJJWEWPOFCCAAIJCLRSMWWBRZZZQKUQILMVLSJDWYBDSRYNLVDIBUELMMGJSUHEFLZZNMVEWDULUILAPTAHRRBNNMJZYVYPEGWCNNROTIZSFJGNMLNGAXELVJHAJSITQKRCYPBIZZTYDWYXEFVWHVOQZQADUYMIYHXQXAABAJEKFSYKVXMEHZILJNIBPBUXAVMBNLEHRRYZKPRRKQXPMYXWDZWRQHMOQGZFRRSNCEUQSIPWFLTDGUYNEJOGTPSPCNFNXQUKTBPKSHUATGSTVDIFYIKKCKMRDDQJYHMOQJGPVVWLNPIRRHGXDEWMDWXLCAZBADNKIQBDFFYRYBIOGEHEHAPZLVAUGWWYJTYKDJCZKPWETFZSSAPKPWPYLVMNHHJMMZCJUJIRSPFPJODMFWMWRCATSROVIMFXQZZNDNKHGFXQHOENVTMNMTUADIGTPBSSXBDRKMPWMJTDLYPOBMTTVKDFYOPTTHIHMTWJVV

肥冰

简单来说,简单应用上无区别;如果是代码复杂量高的,通过第一种可以节省部分时间,因为new需要调用new对象的构造器进行赋值查找。而直接定义则少了这一步,可以说效率略高了一点。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript