1

How an object can be changed after being passed to another function ? For example :

var app = require('express')();
var http = require('http').Server(app);

app.get('/', function (request, response) {
    response.sendFile(__dirname + '/index.html');
});

'http' is already created, using the previously defined 'app'.

Then, a route is set using app.get. But how is that possible ? How the http server will have access to this route defined after assignment ?

trogne
  • 3,402
  • 3
  • 33
  • 50
  • I'm not sure what the actual code here has to do with the question in your title - but to answer the latter, objects in JS (and only objects) are passed "by reference", so they can indeed be altered after passing them into a function. – Robin Zigmond Nov 02 '18 at 16:17
  • @RobinZigmond , ok thanks, but can you show a simple example ? – trogne Nov 02 '18 at 16:18
  • I could, but probably easiest to read this question and answers: https://stackoverflow.com/questions/13104494/does-javascript-pass-by-reference – Robin Zigmond Nov 02 '18 at 16:19

1 Answers1

1

When you pass an object variable as an argument to a function in Javascript , it is passed by reference. So when you make changes to app outside of http the changes are visible in http because you made changes to the same old object reference of which was passed to http.

Consider this example:

function print(obj) { // -- this is Http in your case
    setTimeout (()=> { console.log(obj.a); } , 1000);
}

var my_obj = { a: 100 }; // -- this is app in your case

print(my_obj); // -- this is passing app to http in your case

my_obj.a = 101; // -- this is adding a route to app in your case

There will be 101 logged into console. Because actual object changes before 1000 milliseconds pass. Global context and a function both still reference to the same object. This proves that objects are passed by reference in Javascript.

If you remove the setTimeout, then there will be 100 logged in to console, here is the snippet:

function print(obj) { 
    console.log(obj.a);
}

var my_obj = { a: 100 }; 

print(my_obj);

my_obj.a = 101; 
komron
  • 2,267
  • 2
  • 17
  • 26
  • Ok, does that mean that the javascript engine, when first parsing the file, builds up the "my_obj" object (setting the "a" property and so on), then executes the print function with that object ? – trogne Nov 02 '18 at 16:27
  • @trogne , yes indeed. In given example JS builds the object, then runs the function, but **while the function is running** it changes the property of that object. – komron Nov 02 '18 at 16:34
  • "while the function is running it changes the property of that object." - While it's running ? It's not already changed ? If you remove the timeout, the console.log will still print 101. So I think that it's changed before executing. – trogne Nov 02 '18 at 16:35
  • 1
    @trogne , When it starts executing the function, value of objects `a` property is 100. But inside the function we run asyncronous function setTimeout, which waits for 1000 milliseconds. And while func is waiting for 1000 milliseconds, JS keeps running, and it changes the `a` property of the object to 101, so after 1000 milliseconds are passed the `a` property is already 101, so we see in console 101. – komron Nov 02 '18 at 16:39
  • @trogne you are wrong my friend, if you remove `setTimeout` you will see 100 in console, let me add second snippet to my answer and show you. – komron Nov 02 '18 at 16:41
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/183020/discussion-between-trogne-and-komron). – trogne Nov 02 '18 at 16:42