Solved

Call function

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now