- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to define functions inside a function body in JavaScript?
To achieve what you want, use JavaScript Closures. A closure is a function, which uses the scope in which it was declared when invoked. It is not the scope in which it was invoked.
Example
Let’s take your example and this is how you can achieve your task. Here, innerDisplay() is a JavaScript closure.
Var myFunction = (function () { function display() { // 5 }; function innerDisplay (a) { if (/* some condition */ ) { // 1 // 2 display(); }else { // 3 // 4 display(); } } return innerDisplay; })();
Advertisements