1

Sorry for the title of this question, but I don't found a better one.


INTRODUCTION

My goal is to create an object, and associate to every params a function. Very simple!

var canIDoThis = {
    one: function(a, b, c){},
    two: function(a){},
    three: function(){}
}

I want call canIDoThis in this way

if(canIDoThis['one'](x, y, z)) {
    // do somthing
}

if(canIDoThis['two'](w)) {
    // do somthing
}

if(canIDoThis['three']()) {
    // do somthing
}

If I have a var that can be "one", "two" or "three" I can use this canIDoThis in this way

// myVariable can be equalso to "one" or "two" or "three"
var myVarable;

if(canIDoThis[myVarable]()) {
    // do somthing
}

MY PROBLEM

I'd like to manage all myVarable value. Now if I call canIDoThis['four']() I get obviously Uncaught TypeError: undefined is not a function


MY QUESTION

Is there a way to prevent the default behaviour of Uncaught TypeError and return a default value? It would be cute if canIDoThis['four']() be interpretated as false or undefined

PS: I would like do this in pure JavaScript ;)

4
  • 2
    Why not to use object-oriented way with creating a class-function with getters and stuff? Commented May 29, 2014 at 15:49
  • Object Oriented Way is the most correct way, in my opinion... ;) I war searching a different way. Link add some custom exception handler or something like this. But maybe it's not as simple... Anyway I done it with OO way :) Commented May 30, 2014 at 9:44
  • You'd probably post your solution as an answer here, so others may see the way you found useful and problem solving. Commented May 30, 2014 at 9:48
  • Yep, I'm doping it... ;) Commented May 30, 2014 at 9:48

4 Answers 4

1

Try checking if the property is a function before calling it using typeof.

if (typeof(canIDoThis[myVarable]) === 'function') {
    canIDoThis[myVarable]();
}
Sign up to request clarification or add additional context in comments.

Comments

1

All solutions posted was good turnaround for the problem, but I was searching for something like "Cath the exception and handle it" or "set a default behaviour"...

But this way seem to be not possible.

I prefered to use an Object-Oriented style solution.

This is the solution...

function Context(type) {
    var _type = type,
        // all the functions in config returs true or false, depens on the value of p
        _config = {
            one: function(p){},
            two: function(p){},
            three: function(p){}
        },
        _defaultBehaviour = function() {return false;}
    this.canIDoThis = function(param){
        var fn = this._config[_type] || _defaultBehaviour;
        return fn(param);
    }
}

and then use it in this way

// type can be absolutly anything
var type,
    param = {/*some dynamic params in an Object*/},
    context = new Context(type)

if (context.canIDoThis(params)) {
    // good! Let's go!
} else {
    // stop, access not allowed!
}

This solution work really good for my use case, but It could be improved.

1 Comment

Yes, this is the way to do the things.
0

Check that it's not undefined before trying to call it.

// myVariable can be equalso to "one" or "two" or "three"
var myVarable;
var fn = canIDoThis[myVarable];

if(fn && fn()) { // Only invokes fn if it's not falsey; if it's undefined, it won't try to call it
    // do somthing
}

Check to see if canIDoThis[myVarable] has a value before trying to call it.

Comments

0

Instead of an object, use a "getter" function. That way you can check if the method exists before you try and call it.

function getValue(name){
    var canIDoThis = {
        one: function(a, b, c){},
        two: function(a){},
        three: function(){}
    }

    if(typeof canIDoThis[name] === 'function'){
        return canIDoThis[name].apply(canIDoThis, [].slice.call(arguments, 1));
    }
    else{
        return false;  // Default return value
    }
}

Then you can do getValue('one', x, y, z); or getValue('four');.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.