Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Call function

Posted on 2013-12-15
3
Medium Priority
?
352 Views
Last Modified: 2013-12-31
Is it possible to call a function from within a function? But if function isnt declared do nothing?

function showUrlInDialog(options){
     (options.onOpen || function () { /*Do nothing*/ });
     }

showUrlInDialog({
     myValue1:27,
     myValue2:45,
     onOpen: function () { alert('hello'); }
     });

Open in new window

0
Comment
Question by:tonelm54
3 Comments
 
LVL 53

Assisted Solution

by:COBOLdinosaur
COBOLdinosaur earned 1000 total points
ID: 39720554
No problem if it is a named function:

if(typeof someFunction == 'function') {
    someFunction();
}

If the function is not defined as a function the the execution drops out of the if or you can use an else  to return a false if you wish.

Cd&
0
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 1000 total points
ID: 39720907
Here's a solution for your problem with 3 possible input scenarios.
1. No function at all, not even the property is declared
2. The onOpen is declared but it's not a function
3. onOpen is declared and is a function

Live sample: http://jsfiddle.net/AlexCode/yPB9H/

function showUrlInDialog(options) {
    options = options || {};
    
    if(options.onOpen && typeof options.onOpen === 'function')
        options.onOpen();
    else {
        // you can just return false or undefined here as best describes your logic
        alert('no function!');
    }
}

showUrlInDialog({
    myValue1: 27,
    myValue2: 45
});

showUrlInDialog({
    myValue1: 27,
    myValue2: 45,
    onOpen: 123
});

showUrlInDialog({
    myValue1: 27,
    myValue2: 45,
    onOpen: function () { alert('hello'); }
});

Open in new window

0
 
LVL 9

Expert Comment

by:Sar1973
ID: 39720990
You will get an undefined error if you have not declared the function before.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question