Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

Empowering you to understand your world

Passing Objects As Parameters In Node.js

How to pass an object to another function in Node.js.

Getting Started With Node.js –  Passing Objects As Parameters

Due to the fact that you cannot access objects declared inside another function, you may need to grant other functions access to it. One way to do this in Node.js is by passing the object as a parameter to the function you’d like to use it in. As is indicated by line 1, you will need the http module for this exercise, but don’t worry, it already comes with Node.js.

How to pass an object to another function in Node.js.
How to pass an object to another function in Node.js.

The syntax for that procedure in Node.js is as follows:

sampleFunc(objectToPass);

function sampleFunc(objectToPass) {}

Here’s a working example of that function passing technique:

var http = require('http');

function OnRequest(request, response) {
    sendPage(request, response); //This is where the request and response objects are passed as parameters.
}

function sendPage(request, response) { //Call 'request' and 'response' anything you'd like at this point.
    console.log("Request received.");
    response.writeHead(200, {'Content-Type': 'text/html'});
    response.write('<html>');
    response.write('<head>');
    response.write('<title>My Site</title>');
    response.write('<meta name="viewport" content="width=device-width, initial-scale=1">');
    response.write('</head>');
    response.write('<body>');
    response.write('Hello world!');
    response.write('</body>');
    response.write('</html>');
    response.end();
}

http.createServer(OnRequest).listen(8080); //See what I did there with OnRequest? Functions can be passed like variables too!

In the example code above, the request and response objects are declared in the OnRequest function. This means that they are accessible only from within the OnRequest function. They are passed as parameters to the sendPage function, making them accessible from that function as well. At this point, request and response are local variables, and they are accessible only within those two functions. This technique is an easy way to make your variables/objects accessible wherever you need them, but without declaring them globally. Global variables are not recommended unless necessary.

Further Reading

Learn How JavaScript Functions Work

Learn How JavaScript Variables Work

Official Node.js Docs


Code Successfully Tested Using Node.js Version:

  • 4.2.6.

Leave a Reply

Subscribe to our newsletter
Get notified when new content is published