Javascript:奇怪的语法元素
我很难理解这段Javascript代码:Javascript:奇怪的语法元素,javascript,Javascript,我很难理解这段Javascript代码: var _ref3 = (0, (_asyncToGenerator2 || _load_asyncToGenerator()).default)(function* () { if (actualArgs.length === 0) { const sandbox = yield getValidSandbox(curDir); // It's just a status command. Print the command that wou
var _ref3 = (0, (_asyncToGenerator2 || _load_asyncToGenerator()).default)(function* () {
if (actualArgs.length === 0) {
const sandbox = yield getValidSandbox(curDir);
// It's just a status command. Print the command that would be
// used to setup the environment along with status of
// the build processes, staleness, package validity etc.
let envForThisPackageScripts = PackageEnvironment.calculateEnvironment(sandbox, sandbox.packageInfo, { useLooseEnvironment: true });
console.log(PackageEnvironment.printEnvironment(envForThisPackageScripts));
} else {
let builtInCommandName = actualArgs[0];
let builtInCommand = builtInCommands[builtInCommandName];
if (builtInCommand) {
builtInCommand(curDir, ...process.argv.slice(3));
} else {
console.error(`unknown command: ${builtInCommandName}`);
}
}
});
什么是
\u ref3
?函数?元组?我很困惑我不想为您阅读代码,但我认为,只要有一点帮助,您就可以自己理解此代码。我想您需要帮助了解上面代码中使用的各种新语法。我将试着记下这些,以便您自己能够理解所有这些代码
(0, (_asyncToGenerator2 || _load_asyncToGenerator()).default)(function*{})
这条线基本上类似于
(0,x)(function*{})
其中x是一个函数,它以生成器函数作为参数
每当您有一行(x,y)形式时,它总是返回最后一个值。所以在(x,y)的情况下,它将返回y。如果是(0,x),它将返回x。因此,在您发布的代码中,第一行将返回(_asyncToGenerator2 | | | u load_asyncToGenerator())。默认值
您现在可以将代码翻译为
((_asyncToGenerator2 || _load_asyncToGenerator()).default)(function* {})
这意味着上述代码将返回一个以生成器为参数的函数
如果你需要更多关于发电机的信息,你可以去
生成器函数具有诸如产量之类的属性。它们非常有用,尤其是在处理异步操作时。它简化了您的代码并使其易于阅读。要获得更多关于收益率意味着什么的信息,您可以
您也可以在代码中看到类似的行
builtInCommand(curDir, ...process.argv.slice(3));
这基本上是正在使用的扩展运算符。扩展运算符基本上允许在需要多个参数的地方扩展表达式。你可以去
了解更多有关spread运算符的信息
希望您在理解这些概念后能够自己阅读上述代码。typeof _ref3告诉您什么?@Jamiec,
typeof _ref3
说它是一个函数。谢谢你的技巧。带星号的函数是一个生成器:*这取决于default
return返回的值。你不需要理解这段代码,就像你不需要理解C编译器生成的汇编代码一样,除非你在研究transpilers。但是为什么(a,b)?这仅仅是为了强制计算a
,而是为了返回“b”?@Attilah,是的。