[英]How to translate following nodejs code into ES6
This is my first javascript project (also a first programming project after a decade) so go easy on me.这是我的第一个 javascript 项目(也是十年后的第一个编程项目)所以放轻松。
I am working on VueJS and using a module form nodejs.我正在研究 VueJS 并使用模块形式的 nodejs。 Now, I have a need to add some functionality to the nodejs module.现在,我需要向 nodejs 模块添加一些功能。 I copied relevant files into my project.我将相关文件复制到我的项目中。 However, the files have syntax different from my code (it uses 'use strict;
and require
vs my code that uses export/import.但是,这些文件的语法与我的代码不同(它使用'use strict;
和require
与我使用导出/导入的代码。
The code in question looks like this:有问题的代码如下所示:
'use strict';
var SSH = module.exports;
var Enc = require('./encoding.js');
SSH.parse = function (opts) {
var pub = opts.pub || opts;
var ssh = SSH.parseBlock(pub);
ssh = SSH.parseElements(ssh);
//delete ssh.bytes;
return SSH.parsePublicKey(ssh);
};
/*global Promise*/
SSH.fingerprint = function (opts) {
var ssh;
if (opts.bytes) {
ssh = opts;
} else {
ssh = SSH.parseBlock(opts.pub);
}
// for browser compat
return Promise.resolve().then(function () {
return 'SHA256:' + require('crypto').createHash('sha256')
.update(ssh.bytes).digest('base64').replace(/=+$/g, '');
});
};
...
...
How can I translate this code into ES6 style.如何将此代码转换为 ES6 样式。 Where I can import this and make a call like SSH.fingerprint(publickey)
.我可以在哪里导入它并拨打SSH.fingerprint(publickey)
类的电话。
how about this这个怎么样
exports.funcName = async (params) => {
// your code
}
on other working file在其他工作文件上
const fileName = require('../path')
//call it
fileName.funcName()
Node modules use Commonjs Modules syntax.节点模块使用 Commonjs 模块语法。
In Node, we use require
to import which is equivalent to import
in es6.在 Node 中,我们使用require
来导入,相当于 es6 中的import
。 For export, we use modules.exports
or exports
which is equivalent to the export
keyword in es6 Modules对于导出,我们使用modules.exports
或exports
,相当于es6 Modules 中的export
关键字
import in NodeMoules在 NodeMoules 中导入
var Enc = require('./encoding.js');
es6 Modules es6 模块
import * as Enc from './encoding.js';
export in NodeMoules在 NodeMoules 中导出
const variable = 3;
module.exports = {
variable
};
or或者
exports.variable = 3;
es6 Modules es6 模块
export const variable = 3;
Edit to answer OP's comment编辑以回答 OP 的评论
how do I translate var SSH = module.exports;我如何翻译 var SSH = module.exports;
In Node Modules, whatever you defined on module.exports
object it will be exported by the module.在 Node Modules 中,无论你在module.exports
对象上定义什么,它都会被模块导出。 This means if you define anything on its reference it will also be exported because it is js reference it will point to the same object.这意味着如果您在其引用上定义任何内容,它也将被导出,因为它是 js 引用,它将指向同一个对象。
var SSH = module.exports;
this code, save a reference of module.exports in SSH variable.这段代码,在 SSH 变量中保存了 module.exports 的引用。 if you defined any key on the SSH variable it will be exported ie SSH.fingerprint.如果您在 SSH 变量上定义了任何密钥,它将被导出,即 SSH.fingerprint。
So in order to export fingerprint in es6 Module you just need to do this所以为了在es6模块中导出指纹你只需要这样做
export const fingerprint = function (opts) {
var ssh;
if (opts.bytes) {
ssh = opts;
} else {
ssh = SSH.parseBlock(opts.pub);
}
// for browser compat
return Promise.resolve().then(function () {
return 'SHA256:' + require('crypto').createHash('sha256')
.update(ssh.bytes).digest('base64').replace(/=+$/g, '');
});
};
FYI, exports
keyword in node modules is also a reference to module.exports
仅供参考,节点模块中的exports
关键字也是对module.exports
的引用
Update for the second comment, Your final code will become:更新第二条评论,您的最终代码将变为:
'use strict';
import * as Enc './encoding.js';
export const parse = function (opts) {
const pub = opts.pub || opts;
const ssh = parseBlock(pub);
ssh = parseElements(ssh);
//delete ssh.bytes;
return parsePublicKey(ssh);
};
/*global Promise*/
export const fingerprint = function (opts) {
let ssh;
if (opts.bytes) {
ssh = opts;
} else {
ssh = parseBlock(opts.pub);
}
// for browser compat
return Promise.resolve().then(function () {
return 'SHA256:' + require('crypto').createHash('sha256')
.update(ssh.bytes).digest('base64').replace(/=+$/g, '');
});
};
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.