admin管理员组文章数量:1185683
I wonder how memory is managed in V8. Take a look at this example:
function requestHandler(req, res){
functionCall(req, res);
secondFunctionCall(req, res);
thirdFunctionCall(req, res);
fourthFunctionCall(req, res);
};
var http = require('http');
var server = http.createServer(requestHandler).listen(3000);
The req
and res
variables are passed in every function call, my question is:
- Does V8 pass this by reference or does it make a copy in memory?
Is it possible to pass variables by reference, look at this example.
var args = { hello: 'world' }; function myFunction(args){ args.newHello = 'another world'; } myFunction(args); console.log(args);
The last line,
console.log(args);
would print:"{ hello: 'world', newWorld: 'another world' }"
Thanks for help and answers :)
I wonder how memory is managed in V8. Take a look at this example:
function requestHandler(req, res){
functionCall(req, res);
secondFunctionCall(req, res);
thirdFunctionCall(req, res);
fourthFunctionCall(req, res);
};
var http = require('http');
var server = http.createServer(requestHandler).listen(3000);
The req
and res
variables are passed in every function call, my question is:
- Does V8 pass this by reference or does it make a copy in memory?
Is it possible to pass variables by reference, look at this example.
var args = { hello: 'world' }; function myFunction(args){ args.newHello = 'another world'; } myFunction(args); console.log(args);
The last line,
console.log(args);
would print:"{ hello: 'world', newWorld: 'another world' }"
Thanks for help and answers :)
Share Improve this question edited Jul 16, 2018 at 9:07 roschach 9,33617 gold badges88 silver badges137 bronze badges asked Aug 12, 2012 at 15:36 onlineracoononlineracoon 2,9705 gold badges48 silver badges66 bronze badges1 Answer
Reset to default 27That's not what pass by reference means. Pass by reference would mean this:
var args = { hello: 'world' };
function myFunction(args) {
args = 'hello';
}
myFunction(args);
console.log(args); //"hello"
And the above is not possible.
Variables only contain references to objects, they are not the object themselves. So when you pass a variable that is a reference to an object, that reference will be of course copied. But the object referenced is not copied.
var args = { hello: 'world' };
function myFunction(args){
args.newHello = 'another world';
}
myFunction(args);
console.log(args); // This would print:
// "{ hello: 'world', newHello: 'another world' }"
Yes that's possible and you can see it by simple running the code.
本文标签: javascriptNodejs V8 pass by referenceStack Overflow
版权声明:本文标题:javascript - Node.js V8 pass by reference - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738345294a2077947.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论