Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Call function

Posted on 2013-12-15
3
Medium Priority
?
345 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

722 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