
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Is there any way I can call the validate() function outside the initValidation() function in JavaScript?
We wish to call the function validate() outside of initValidation(), without necessarily having to call initValidation()
Following is our problem code −
function initValidation(){ // irrelevant code here function validate(_block){ // code here } }
In JavaScript, as we know that functions are nothing but objects, so to achieve this we can tweak our code like this −
function initValidation(){ // irrelevant code here function validate(_block){ // code here console.log(_block); } this.validate = validate; }
What this tweak does is that it makes our parent function to represent a class now, of which validate is a property and we can access it like this −
const v = new initValidation(); v.validate('Hello world');
Following is the complete code with output −
Example
function initValidation(){ // irrelevant code here function validate(_block){ // code here console.log(_block); } this.validate = validate; } const v = new initValidation(); v.validate('Hello world');
Output
The output in the console will be −
Hello world
- Related Articles
- How to call a jQuery plugin function outside the plugin?
- JavaScript Function Call
- How to call jQuery function with JavaScript function?
- How do I call a JavaScript function on page load?
- JavaScript outsider function call and return the result
- How to call a function inside a jQuery plugin from outside?
- How to call a Java function inside JavaScript Function?
- Can main function call itself in C++?
- How to call a function in JavaScript?
- Call a function with onclick() – JavaScript?
- How to call a function that returns another function in JavaScript?
- What is the best way to do optional function parameters in JavaScript?
- Is there any whoami function or command in MySQL like UNIX?
- How can I check if a JavaScript function is defined?
- How to use setInterval function call in JavaScript?

Advertisements