Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于装饰器章节这个说法的疑问:「装饰器对类的行为的改变,是代码编译时发生的,而不是在运行时」 #1165

Closed
xcatliu opened this issue Aug 18, 2023 · 1 comment

Comments

@xcatliu
Copy link
Contributor

xcatliu commented Aug 18, 2023

请问为啥说「装饰器对类的行为的改变,是代码编译时发生的,而不是在运行时」?

这里的「代码编译时」指的是「ts/esnext 编译为 js」,还是指「js 运行过程中的编译阶段」?

如果是指的「ts/esnext 编译为 js」,那么我测试了一下 babel 和 tsc,编译出来的代码中 testable 装饰器是在运行时才会执行的。

如果是指「js 运行过程中的编译阶段」,那目前还没有浏览器/Node 能够直接运行 decorator,执行的都是 ts/esnext 转为 js 后的代码。

这个是因为历史原因么?以前的 babel 编译会在编译阶段执行?

@ruanyf
Copy link
Owner

ruanyf commented Aug 19, 2023

JS 引擎以前不支持装饰器,“编译”就是指在 Babel/TS 转换代码时,会处理掉装饰器。

JS 引擎现在支持了,这样说就不对了,应该说装饰器是在引擎“评估”(evalute)代码时执行。我把这行删了。

@ruanyf ruanyf closed this as completed in e8c2d2e Aug 19, 2023
github-actions bot pushed a commit to shaul-xu/es6tutorial that referenced this issue Aug 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants