How to check if a variable is loaded in JavaScript?

后端 未结 10 1687
遇见更好的自我
遇见更好的自我 2021-01-31 04:01

How do I see if a certain object has been loaded, and if not, how can it be loaded, like the following?

if (!isObjectLoaded(someVar)) {
    someVar= loadObject()         


        
10条回答
  •  再見小時候
    2021-01-31 04:43

    If it is an object then you should just be able to check to see if it is null or undefined and then load it if it is.

    if (myObject === null || myObject === undefined) {
       myObject = loadObject();
    }
    

    Using the typeof function is also an option as it returns the type of the object provided. However, it will return null or undefined if the object has not been loaded so it might boil down a bit to personal preference in regards to readability.

提交回复
热议问题