Does JavaScript support some kind of __Resolve method?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Arodicus
    New Member
    • May 2007
    • 12

    Does JavaScript support some kind of __Resolve method?

    I have a dynamic class. I would like to be able to attempt to access properties and methods on that class, and if they do not exist, I want the Class to handle the problem in a specific way, rather than throw an exception.

    Thsi needs to be done internally to the class, not with an external "try" clause. In some languages this is known as a __resolve function.

    thus

    [CODE=javascript]var foo = new MyClass()

    function MyClass(){
    this.dog = "woof"
    function __resolve(e) {
    alert("I don't know what a" + e + "is");
    }
    }

    alert(foo.dog) // woof
    foo.cat() // I don't know what a cat is[/CODE]

    Is this possible?
    Last edited by gits; Apr 4 '08, 03:27 PM. Reason: fix code tags
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hmmm ... at the moment for a quick shot i would use something like the following for that:

    [CODE=javascript]
    function OBJ() {
    this.foo = function(s) {
    alert(s);
    };
    }

    OBJ.prototype.f = function(func, params) {
    if (typeof this[func] != 'undefined') {
    this[func].apply(this, params);
    } else {
    alert('i don\'t know what ' + func + ' is.');
    }
    }

    var o = new OBJ;

    o.f('foo', ['test']);
    [/CODE]
    that example calls foo like expected and when you try to call:

    [CODE=javascript]o.f('cat', []);[/CODE]
    you get it '__resolved' ...

    may be that helps?

    kind regards

    PS: may be that has to be adapted for different things, when trying to call a method (like the example works at the moment) or a simple property or whatever ;)

    Comment

    Working...