分享一個js技巧!判斷一個變量chat_websocket是否存在。


注意!!!

判斷一個變量chat_websocket是否存在:
if( "undefined" == typeof(chat_websocket) || null == chat_websocket ){}
把值放在前面,變量放在后面

而不是像下面這樣寫:

if( typeof(chat_websocket) == "undefined" || chat_websocket == null ){}
雖然等價,但是呢像上面的寫法被認為是技術大牛、有經驗的人,因為上面的寫法可以避免你一不小心將代碼寫成這樣:if( typeof(chat_websocket) = "undefined" || chat_websocket = null ){},的時候出現報錯。寫成這樣:if( "undefined" = typeof(chat_websocket) || null = chat_websocket ){}是不會報錯的。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM