Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
if (foo) { // code to run if foo exists }
导致 Uncaught ReferenceError: foo is not defined
那么我应该如何检查是否foo存在?
foo
有几种方法 - 一种是
if(typeof foo !== 'undefined'){ // your code here. }
JS FIDDLE 用来检查