1
var myNamespace = {
    dateController: {}
};

myNamespace.dateController = function(callback) {
    this.callbackfunction = callback;
    try {
        [this.callbackfunction]();
    } catch (e) {
        alert(e);
    }
};

function displayDate() {
    alert("displayDate");
    myNamespace.dateController("displayDateFromController");
};

function displayDateFromController() {
    alert("In displayDateFromController");
};    

This piece of code is giving me TypeError: ["displayDateFromController"] is not a function error. What could be root cause and possible solution to this issue.

Why dateController not able to identify displayDateFromController as function.

I have tired this on http://www.w3schools.com/js/tryit.asp?filename=tryjs_events

2
  • several issues: 1) leave out the []. What you are actually doing is putting this.callbackfunction in an array and trying to call that array as a function. 2) don't pass the functionName as a string to your dateController function. Instead just pass the function itself: myNamespace.dateController(displayDateFromController); Commented Jun 3, 2013 at 8:38
  • I have tired out that also... this.callbackfunction() still giving me same error. Commented Jun 3, 2013 at 8:41

2 Answers 2

2

You need to pass the actual function to the datecontroller method instead of the String.

var myNamespace = {
    dateController: {}
};

myNamespace.dateController = function (callback)
{
 this.callbackfunction = callback;
 try{
    //remove [] surrounding function
    this.callbackfunction();
    }
    catch(e)
    {
      alert(e);
    }
};

//Declare this method prior to displayDate
function displayDateFromController()
{
    alert("In displayDateFromController");
};

function displayDate()
{
    alert("displayDate");
    //Pass function instead of string
    myNamespace.dateController(displayDateFromController); 
};

displayDate();

Working Example: http://jsfiddle.net/RDMHV/

If you still need the flexibility of a String:

var myNamespace = {
    dateController: {}
};

myNamespace.dateController = function (callback)
{
 this.callbackfunction = this[callback];
 try{
    this.callbackfunction();
    }
    catch(e)
    {
      alert(e);
    }
};

myNamespace.displayDateFromController = function(){
   alert("In displayDateFromController");
};

function displayDate()
{
    alert("displayDate");
    myNamespace.dateController("displayDateFromController");
};

displayDate();

Working Example http://jsfiddle.net/RDMHV/1/

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

2 Comments

thanks @Kevin for prompt reply... I can relate your answer with function pointer fundamental. I know that but in this case i have some specific requirement in which i have to keep function name in string. Will it possible to still passing function name as string and calling the same from controller.
Is it necessary to have displayDateFromController in myNamespace in your second codebase? can't i have similar to displayDate? Actually i am relating this issue with global namespace and myNamespace scope issue. displayDateFromController is part of globale namespace thats why it is not detecting function from string.
0

You have to remove the brackets around the call :

try{
    this.callbackfunction();
}
catch(e)
{
    alert(e);
}

and to pass the function without the quotes :

function displayDate()
{
    alert("displayDate");
    myNamespace.dateController(displayDateFromController);
 };

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.