gpt4 book ai didi

npm - 谁能解释一下 es7 反射元数据是什么?

转载 作者:行者123 更新时间:2023-12-01 23:24:19 25 4
gpt4 key购买 nike

已经研究 ES6、JSPM 和 angular2 一周了,我发现了这个 repo ES6-loader

如果我们查看底部脚本的index.html,您会看到

 System.import('reflect-metadata')
.then(function() {
return System.import('app/index');
})
.catch(console.log.bind(console));

这是使用 JSPM 的 systemjs polyfill 来获取 ES6 的导入

问题:在这种情况下,反射元数据到底做什么? npm reflect-meta我阅读文档越多,就越不了解它的作用?

最佳答案

'reflect-metadata' 是一个针对 ES7 的提案的包。它允许将元数据包含到类或函数中;本质上是 syntax sugar .

示例。Angular 2 ES6:

@Component({selector: "thingy"})
@View({template: "<div><h1>Hello everyone</h1></div>"})
class Thingy{};

如您所见,@Component 和 @View 后面没有分号。这是因为它们本质上是类上的(元)数据链。

现在让我们看看 Angular 2 和 ES5 中的相同代码:

function Thingy(){}
Thingy.annotations = [
new angular.ComponentAnnotation({
selector: "thingy"
}),
new angular.ViewAnnotation({

template: "<div><h1>Hello everyone</h1></div>"
})
];

正如你所看到的,@符号抽象出了类的很多注释属性,并使其变得更多D.R.Y .

更进一步,Angular 团队知道注释对于新用户来说有点抽象。而且,ES5 实在是太冗长了。这就是为什么他们制作了 a.js ,这将使与注释的接口(interface)更好:

Video to understand this

关于npm - 谁能解释一下 es7 反射元数据是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30539571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com