包范围是如何实现的? [英] How is package scope achieved?

查看:24
本文介绍了包范围是如何实现的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Meteor 文档(参见 Namespacing 部分)说:

Meteor documentation (see section Namespacing) says:

当你声明一个顶级变量时,你有一个选择.您可以将变量设为 File Scope 或 Package Scope.

When you declare a top-level variable, you have a choice. You can make the variable File Scope or Package Scope.

// File Scope. This variable will be visible only inside this
// one file. Other files in this app or package won't see it.
var alicePerson = {name: "alice"};

// Package Scope. This variable is visible to every file inside
// of this package or app. The difference is that 'var' is
// omitted.
bobPerson = {name: "bob"};

这真是太好了.如果一个包有多个源代码文件,这些文件可以共享变量但将世界的其他部分排除在外.然而我开始怀疑.

This is really nice. If a package has more than one source code file, the files can share variables but keep the rest of the world out. However I started wondering.

他们到底是如何实现包范围的?

起初我以为他们是通过立即调用的函数表达式来实现的.你知道这样的结构:

At first I thought they achieved it with immediately invoked function expressions. You know constructs like this:

(function() { /* your code here */ })();

所以我尝试在控制台中模拟这种行为.我定义了一个这样的变量:x = "Package scopeCandidate";.

So I tried to emulate this behaviour in the console. I define a variable like this: x = "Package scope candidate";.

我在控制台输入:

(function() {
    x = "Package scope candidate";
})();
console.log(x);

并且我在控制台中得到 Package scopeCandidate".多么令人失望.变量 x 落在全局范围内.在 Window 对象中.绝对不是我想要的.还有流星?他们以某种方式管理它.他们一定使用了一些我不明白的魔法.

and I get "Package scope candidate" in the console. How disappointing. The variable x landed in the global scope. In the Window object. Absolutely not what I wanted. And Meteor? They managed it somehow. They must have used some magic I don't understand.

推荐答案

看起来他们重新定义了文件顶部的变量.

It looks like they redefine the variables at the top of the file.

例如:

x = "Package scope candidate"

会变成

(function() {
    var x;
    (function() {
        x = "Package scope candidate";
    }).call(this);
}).call(this);

有一个解释器会查找变量并将它们添加到顶部,以便它们成为文件的范围,这反过来又反映了包的范围.

There's an interpreter that looks for the variables and adds them in at the top so that they become scoped to the file, which in turn reflects the packages scope.

如果你看看 https://github.com/meteor/meteor/blob/devel/tools/linker.js 你可以找到一个叫做 jsAnalyze 的东西,它在文件的顶部查找全局变量以重新定义文件的范围.在将文件提供给客户端之前,它会以上述方式重新打包.

If you look at https://github.com/meteor/meteor/blob/devel/tools/linker.js you can find something called jsAnalyze that looks around the file for global variables to rescope at the top of the file. Before the file is served to the client it is repackaged in a way like above.

jsAnalyze 包做这个看看://github.com/meteor/meteor/blob/94c7833c82ac6ab62c36b4a39ee315199233aef9/packages/js-analyze/js_analyze.js

The jsAnalyze package does this have a look at https://github.com/meteor/meteor/blob/94c7833c82ac6ab62c36b4a39ee315199233aef9/packages/js-analyze/js_analyze.js

所以全局变量被提取&该文件使用全局变量、导入和导出进行重组,这就是其命名空间自行关闭的方式.

So the globals are extracted & the file is restructured with the globals, imports, and exports and this is how its namespaced off on its own.

这篇关于包范围是如何实现的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆