Is there a way to catch an attempt to access a non existant property or method?

后端 未结 4 1517
星月不相逢
星月不相逢 2020-12-06 00:56

For instance this code:

function stuff() {
  this.onlyMethod = function () {
    return something;
  }
}

// some error is thrown
stuff().nonExistant();
         


        
4条回答
  •  一整个雨季
    2020-12-06 01:14

    You can also check if the method exists.

    if(a['your_method_that_doesnt_exist']===undefined){
    //method doesn't exist
    }
    

提交回复
热议问题