本文介绍了使用vm.runInNewContext创建类(强制类表达式而不是ES6中的类声明)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调试在 Atom 编辑器中运行的另一个人的程序包(当前未维护).某个时刻,该程序包使用 vm.runInContext 定义类对象,并使用与以下代码等效的代码:

I am debugging another person's package (currently unmaintained) that runs in the Atom editor. At one point, this package uses vm.runInContext to define a class object, using code equivalent to the following:

const vm = require('vm')
myClass = vm.runInNewContext('class x {}', {}, 'file')

我认为这段代码可以在 node.js 中运行,但是我并不真正了解Atom javascript环境的所有细节.无论如何,我很确定它运行在ES6或更高版本中.我相信,当此软件包的最后一次更新是在2017年4月时,对 vm.runInNewContext 的调用返回了一个有效的类对象.但是,它当前返回 undefined ,这可能是因为自那时以来Atom进行了一些升级(可能在2018年6月1日左右).因此,现在对 myClass 的属性的引用失败,这是我要修复的错误.

I think this code runs in node.js, but I don't really know all the details of Atom's javascript environment. At any rate, I'm pretty sure it's running in ES6 or later. I believe that when this package was last updated in April 2017, the call to vm.runInNewContext returned a valid class object. However, it currently returns undefined, probably due to some upgrade in Atom since then (maybe around June 1, 2018). Because of this, references to attributes of myClass now fail, which is the bug I am trying to fix.

我相信这里发生的事情是javascript将 class x {} 代码解释为没有值的类声明,因此 vm.runInNewContext 返回未定义.因此,我正在寻找一种强制该代码返回类而不是 undefined 的优美方法.我发现了以下可能性:

I believe what is happening here is that javascript interprets the class x {} code as a class declaration, which has no value, so vm.runInNewContext returns undefined. So I'm looking for a graceful way to force this code to return a class, rather than undefined. I have found the following possibilities:

// add a reference to the class, which will be returned as the final value
vm.runInNewContext('class x {}; x', {}, 'file')

// use an assignment, which treats this as a class expression,
// then the assignment is returned as the final value
vm.runInNewContext('var x; x = class x {}', {}, 'file')
// note: a simpler version ('var x = class x {}') returns undefined

// wrap the class statement in parentheses, so it is evaluated as
// a class expression instead of a class declaration
vm.runInNewContext('(class x {})', {}, 'file')

所有这些工作,但它们似乎都很笨拙.是否有某种标准"方式强制javascript/ES6将 class x {} 视为类表达式而不是类声明?

All of these work, but they all seem clunky. Is there some "standard" way to force javascript / ES6 to treat class x {} as a class expression rather than a class declaration?

推荐答案

此行为并非特定于Node.js vm.runInNewContext .该代码应被强制作为表达式被撤消.

This behaviour isn't specific to Node.js vm.runInNewContext. The code should be forced to be evaulated as expression.

eval('function foo () {}') === undefined

因为它被视为函数声明而不是函数表达式.出于相同的原因,此行将导致语法错误:

because it's treated as function declaration instead of function expression. For the same reason this line will result in syntax error:

function () {};

这不是有效的函数声明,也不用作表达式.

It isn't valid function declaration and it's not used as expression.

所以括号

vm.runInNewContext('(class x {})', {}, 'file')

或逗号运算符

vm.runInNewContext('0, class x {}', {}, 'file')

是使评估代码成为表达式的常规方法.

are conventional ways to make evaluated code an expression.

这篇关于使用vm.runInNewContext创建类(强制类表达式而不是ES6中的类声明)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 01:42