Node.js V8通过引用传递

我很好奇如何在V8中完成内存pipe理,看看这个例子:

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); 

每个函数调用都会传递“req”和“res”variables,我的问题是:

V8是否通过引用来传递它,或者是否在内存中进行复制? 2.是否可以通过引用来传递variables,请看这个例子。

 var args = { hello: 'world' }; function myFunction(args){ args.newHello = 'another world'; } myFunction(args); 

的console.log(参数); //这将打印:

 "{ hello: 'world', newWorld: 'another world' }" 

感谢您的帮助和解答:)

这不是通过参考手段。 通过参考将意味着这一点:

 var args = { hello: 'world' }; function myFunction(args) { args = 'hello'; } myFunction(args); console.log(args); //"hello" 

而以上是不可能的。

variables只包含对象的引用,它们本身不是对象。 所以当你传递一个variables作为对象的引用时,这个引用当然会被复制。 但是引用的对象不会被复制。


 var args = { hello: 'world' }; function myFunction(args){ args.newHello = 'another world'; } myFunction(args); console.log(args); // This would print: // "{ hello: 'world', newHello: 'another world' }" 

是的,这是可能的,你可以通过简单的运行代码来看到它。

Interesting Posts