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

Posted by Tor Valamo on Stack Overflow See other posts from Stack Overflow or by Tor Valamo
Published on 2010-04-19T10:27:53Z Indexed on 2010/04/19 10:33 UTC
Read the original article Hit count: 153

Filed under:

For instance this code:

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

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

Is there a way to do something like PHP's __call as a fallback from inside the object?

function stuff() {
  this.onlyMethod = function () {
    return something;
  }
  this.__call__ = function (name, params) {
    alert(name + " can't be called.");
  }
}

// would then raise the alert "nonExistant can't be called".
stuff().nonExistant(); 

© Stack Overflow or respective owner

Related posts about JavaScript