javascript
JS报错-Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on...
報(bào)錯(cuò)信息:Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them;
也就是嚴(yán)格模式下,'caller', 'callee' 和??'arguments'不被允許,只能換一種方式寫(xiě)了。
我項(xiàng)目之前都是OK的,沒(méi)報(bào)這個(gè)錯(cuò)誤。后來(lái),由于gitlab賬號(hào)的密碼修改了,我又沒(méi)配置永久的,所以需要重新拉代碼。
pull完npm install出錯(cuò)(其實(shí)它有時(shí)就會(huì)出現(xiàn)一些不知名的問(wèn)題,與項(xiàng)目并無(wú)關(guān)系的)。于是我用了yarn,然后打包上傳服務(wù)器在手機(jī)上測(cè)試時(shí),就報(bào)了上述錯(cuò)誤。代碼我沒(méi)改過(guò),本地開(kāi)發(fā)也是OK的,經(jīng)過(guò)排查是打包問(wèn)題(我懷疑是yarn install時(shí),肯能某些文件install了嚴(yán)格模式,也說(shuō)不過(guò)去,我也沒(méi)找到證據(jù)? ?/手動(dòng)哭笑)。
還能咋滴,我只能改代碼咯。。。
(因?yàn)槲以谧咏M件向父組件emit一個(gè)方法,但是有2個(gè)參數(shù),父組件在接收的時(shí)候,不能寫(xiě)兩個(gè)參數(shù),so,用了arguments)
原有代碼
1.子組件
//子組件--vue文件 pick(date,_index){let hxDate = '';if(this.type == 'DAY'){ hxDate= timeFormat(date,'YMD')}if(this.type == 'WEEK'){ hxDate= String(date['wYear']) + String(date['wName'])}if(this.type == 'MONTH'){ hxDate= date['date']}if(this.type == 'YEAR'){ hxDate= date['date']} //傳了兩個(gè)參數(shù)this.$emit('getDate',hxDate,_index) }2.父組件
//父組件--html文件(調(diào)用子組件) <date-calendar @getCalendar="getCalendarSale" @getDate="getDateT(arguments)" :type="type">// 代碼省略</date-calendar> //父組件---.vue文件 // 點(diǎn)擊年月進(jìn)詳情頁(yè)面getDateT(value){console.log('點(diǎn)擊年月詳情getDate',value);}3.打印結(jié)果
瀏覽器完全OK,打包就報(bào)Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them。。。氣哭 T_T
?
現(xiàn)有代碼
1.子組件
//子組件--vue文件 pick(date,_index){let hxDate = '';if(this.type == 'DAY'){ hxDate= timeFormat(date,'YMD')}if(this.type == 'WEEK'){ hxDate= String(date['wYear']) + String(date['wName'])}if(this.type == 'MONTH'){ hxDate= date['date']}if(this.type == 'YEAR'){ hxDate= date['date']}//將兩個(gè)參數(shù)組成數(shù)組再傳!!!let hGetDate = [];hGetDate.push(hxDate);hGetDate.push(_index);this.$emit('getDate',hGetDate) }2.父組件
//父組件--html文件(調(diào)用子組件,一個(gè)參數(shù)可省略不寫(xiě)) <date-calendar @getCalendar="getCalendarSale" @getDate="getDateT" :type="type">// 代碼省略</date-calendar> //父組件---.vue文件 // 點(diǎn)擊年月進(jìn)詳情頁(yè)面getDateT(value){console.log('現(xiàn)有點(diǎn)擊年月詳情getDate',value);}3.打印結(jié)果
?
?改成現(xiàn)有代碼之后,打包上傳服務(wù)器在手機(jī)上測(cè)試,OK,完美~
在查找過(guò)程中,也發(fā)現(xiàn)一個(gè)不錯(cuò)的小tips:JS嚴(yán)格模式(use strict)下不能使用arguments.callee的替代方案,分享一下:
//一般在非嚴(yán)格模式下遞歸調(diào)用"use strict"; function factorial(num){if(num<=1){return 1;}else {return num * arguments.callee(num-1);} }console.log(factorial(4));報(bào)錯(cuò)了,也是上述錯(cuò)誤。//在嚴(yán)格模式下不能通過(guò)腳本訪問(wèn)arguments.callee,訪問(wèn)這個(gè)屬性會(huì)報(bào)錯(cuò),那么可以使用命名函數(shù)表達(dá)式來(lái)達(dá)到相同的結(jié)果,如下: "use strict"; var factorial = (function f(num){if(num<=1){return 1;}else {return num * f(num-1);} })console.log(factorial(4)); //24,正常打印再看一段代碼
(function foo(bar) {if (bar) {return;}foo(true); })();后記
以后遇到上述錯(cuò)誤,一定要在相關(guān)代碼處查看你是否使用了'caller', 'callee', and 'arguments' ,然后換其他方式寫(xiě)。
歡迎大佬指出不對(duì)的地方,多交流相互學(xué)習(xí)呀~
?
?
總結(jié)
以上是生活随笔為你收集整理的JS报错-Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: VMware Workstation安装
- 下一篇: JavaScript- 正则表达式匹配汉