[英]Why doesn't this throw an error [JavaScript]?
如果您在控制台中運行此代碼:
(function(){
test: "hello";
})();
甚至這個:
test: "hello";
沒有任何反應,也沒有錯誤。 為什么? 這很簡單,“嗯......這是JavaScript ......處理它。”?
對於上下文,我公司的一些實習生偶然寫了類似的東西(意思是使用'=')。 當面對它可能會拋出錯誤時,他們說它沒有。
而現在我很好奇。
JavaScript中的任何語句(包括創建字符串文字但不對其執行任何操作的語句)都可以有標簽(請參閱ECMAScript 2015或MDN )。 除非您有想要break
或continue
嵌套循環,否則它沒什么用處。
(function(){ // Begin function expression
test: // Label statement
"hello"; // Create a string and do nothing with it
})(); // End function expression and invoke the created function
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.