Solved

Call function

Posted on 2013-12-15
3
296 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
Comment Utility
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
Comment Utility
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
Comment Utility
You will get an undefined error if you have not declared the function before.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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-…
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…

743 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

14 Experts available now in Live!

Get 1:1 Help Now