Join the Stack Overflow Community
Stack Overflow is a community of 6.3 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have following javascript code

function MyFunc () {

    var add = function () {
        return "Hello from add";
    };

    var div = function () {
         return "Hello from div";
    };

    var funcCall =  function (obj) {

        if (!obj) {
            throw new Error("no Objects are passed");
        }
      return obj.fName();


    };

  return {
    func: function (obj) {
      funcCall(obj);
    }
  };

}

var lol = new MyFunc();

When lol.func({fName: add}); is passed it should invoke the function private function add or when lol.func({fName: div}); is passed it should invoke the private div function. What i have being tried does not work. How can i achieve this.

DEMO

share|improve this question
    
possible duplicate of JavaScript property access: dot notation vs. brackets? – Kyll Jul 22 '15 at 14:07
    
I guess only eval could do what you want without updating your code a bit. – Hacketo Jul 22 '15 at 14:09
1  
@Kyll This is not what i have asked :) – sam Jul 22 '15 at 14:11
up vote 3 down vote accepted

In this case it's better to store your inner function in the object so you can easily access this with variable name. So if you define a function "map"

var methods = {
    add: add,
    div: div
};

you will be able to call it with methods[obj.fName]();.

Full code:

function MyFunc() {

    var add = function () {
        return "Hello from add";
    };

    var div = function () {
        return "Hello from div";
    };

    var methods = {
        add: add,
        div: div
    };

    var funcCall = function (obj) {

        if (!obj) {
            throw new Error("no Objects are passed");
        }

        return methods[obj.fName]();
    };

    return {
        func: function (obj) {
            return funcCall(obj);
        }
    };

}

var lol = new MyFunc();
console.log( lol.func({fName: 'add'}) );
share|improve this answer
    
This is great !!!! Thank you so much :) – sam Jul 22 '15 at 14:16

When you pass lol.func({fName: add}) add is resolved in the scope of evaluating this code, not in the scope of MyFunc. You have to either define it in that scope like:

function MyFunc () {

    var add = function () {
        return "Hello from add";
    };

    var div = function () {
         return "Hello from div";
    };

    var funcCall =  function (obj) {

        if (!obj) {
            throw new Error("no Objects are passed");
        }
      return obj.fName();


    };

  return {
    add: add,
    div: div,
    func: function (obj) {
      funcCall(obj);
    }
  };

}
var lol = new MyFunc();
lol.func({fName: lol.add});

Or use eval.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.