返回Node.js中的多个参数

假设我们在Node.js中有一个函数,那么返回这两个参数的正确方法是什么?

例如,我有一个函数返回一个encryption的消息,就像上面的代码,我想返回也Hmac散列它将被复制。 我可以从一个函数返回两个值吗?

const crypto = require('crypto'); exports.AesEncryption = function(Plaintext, SecurePassword) { var cipher = crypto.createCipher('aes-128-ecb', SecurePassword); var encrypted = cipher.update(Plaintext, 'utf-8', 'base64'); encrypted += cipher.final('base64'); return encrypted; }; 

你可以通过使用一个数组来返回两个值:

 const crypto = require('crypto'); exports.AesEncryption = function(Plaintext, SecurePassword) { var cipher = crypto.createCipher('aes-128-ecb', SecurePassword); var encrypted = cipher.update(Plaintext, 'utf-8', 'base64'); encrypted += cipher.final('base64'); return [encrypted, cipher]; }; 

或者一个对象(首选):

 const crypto = require('crypto'); exports.AesEncryption = function(Plaintext, SecurePassword) { var cipher = crypto.createCipher('aes-128-ecb', SecurePassword); var encrypted = cipher.update(Plaintext, 'utf-8', 'base64'); encrypted += cipher.final('base64'); return {encrypted: encrypted, cipher: cipher}; };