2

I'm looking for a way to catch Exceptions when accessing missing properties in the object itself.

I'm looking for something like:

var objOne = 
{
    objTwo: {},
    objThree: {},
    fallback: function(missingProperty)
    {
        alert('obj did not contain property ' + missingProperty);
    }
}

Calls like objOne.objFour should be linked to objOne.fallback('objFour'). Is there a good way to handle this?

1

2 Answers 2

1

Accessing an undefined property does not trigger any exception, you can have the same behaviour with ||:

function callBack (missingProperty) {
  alert('obj did not contain property ' + missingProperty)
}
objOne[attr] || callBack(attr);

This will trigger the callBack function when objOne does not contain attr.

Sign up to request clarification or add additional context in comments.

Comments

0

the idea i have is to modify the constructor of the prototype of your customObject (for example objThree) and check for set values etc...

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor

http://www.w3schools.com/js/js_object_definition.asp

or you define a custom event which is fired by creating an object:

https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events

i hope i understood you correctly and this is what you are searching for. Other possibility would be:

var objThree = function(a, b){
var parent = this;
this.valA = a;
this.valB = b;
this.checkForValues = function(){
   if (parent.valA == null || parent.valB == null) return false;
   return true;
}

greetings

1 Comment

didn't have a clue - thanks for nice idea, upvote it to 0 ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.