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

注意!!!web

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

而不是像下面這樣寫:socket

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

spa

相關文章
相關標籤/搜索