2

What works:

Having a JavaScript object like e.g.

var obj = {
    var1: "one"
};

I can call it like one of the following ways:

console.log(obj.var1);
console.log(obj['var1']);   

What fails:

This one would fail:

console.log(obj.var2);

because var2 is no property/variable of the anonymous object, thus, undefined is printed in the console log.

My goal:

What I would love to have is a fallback function that would be called automatically when no matching property/variable is found.

E.g. something like:

var obj = {
    var1: "one",
    __propertyNotFound__: function (name) {
        if ( name=="var2" ) return "two";
        else return null;
    }
};

Unfortunately I found no whatsoever close solution.

My question:

Being a rather JavaScript newbie, is my question a dumb question and the complete wrong approach or could it make sense and is there actually a solution to solve this?

Uwe Keim
  • 36,867
  • 50
  • 163
  • 268
  • 1
    As far as I know, so far, this is not possible. – basilikum Apr 23 '14 at 21:13
  • 2
    It should be possible with [ES6 Proxies](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-proxy-object-internal-methods-and-internal-slots). But, until then, no. JavaScript hasn't offered support for a default method. [Property accessors](http://ecma-international.org/ecma-262/5.1/#sec-11.2.1) simply return `undefined` if the property isn't found. – Jonathan Lonowski Apr 23 '14 at 21:15
  • exact duplicate of [Is there an equivalent of the \_\_noSuchMethod\_\_ feature for properties, or a way to implement it in JS?](http://stackoverflow.com/questions/2266789/is-there-an-equivalent-of-the-nosuchmethod-feature-for-properties-or-a-way) – Bergi Apr 24 '14 at 00:20

2 Answers2

3

Well, won't be really automatic but you could do something like this:

var obj={
    a:2,
    b:5,
    get:function(index){
        if(this.hasOwnProperty(index)){
            return this[index];
        }else{
            return this.notFound(index);
        } 
    },
    notFound:function(index){
        if ( index=="var2" ) return "two";
        else return null;
    }
}

console.log(obj.get('var2')) // two

You will have to use .get to access properties, but you can handle not found with that way

juvian
  • 15,212
  • 2
  • 30
  • 35
  • 1
    This doesn't actually solve OPs problem. He's trying to call a function when referencing an undefined property on an object, not determine if it will be undefined. – Mike Cluck Apr 23 '14 at 21:22
  • @MikeC yeah understood wrongly. Updated answer, not a good practice but should work for that case – juvian Apr 23 '14 at 23:45
1

Instead of directly referring to the property when you try to retrieve its value, implement a getter function that will be able to return a requested property, as well as perform any desired operation in case you request an undefined property.

Alexander
  • 659
  • 1
  • 5
  • 16
  • Could you give a small example for me to understand? – Uwe Keim Apr 24 '14 at 04:29
  • 1
    Sure. Just to illustrate, so it's a bit dirty. var obj = { _values: {}, get: function(name) { var v = this._values[name]; if (v !== undefined) return v; else alert("error"); } }. You then have the liberty to replace the alert by any code you wish to handle undefined values. – Alexander Apr 24 '14 at 17:05
  • Thanks, I've [tried it here](http://jsfiddle.net/UweKeim/4zgbQ/), without success. – Uwe Keim Apr 24 '14 at 17:54
  • like I said, you should NOT reference properties directly. Instead of x = obj.amIHere use x = obj.get('amIHere'); – Alexander Apr 24 '14 at 18:56