Solved

Call function

Posted on 2013-12-15
3
321 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 250 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 250 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Explain how this javascript function works 1 47
add a 3rd selection to sum 7 29
how can i check if the text is an Isogram ? 2 26
Javascript Form Change 5 23
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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 …
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…

809 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