TypeScript中如何区分模块级变量和局部变量
How to distinguish between module level variables and local variables in TypeScript
考虑这个模块:
export module Example{
let customer : any;
export function myExample(customer: string) {
// How to reference the module level customer object here?
// Is there a standard to make these module level variables Pascal Case to prevent this overlap?
}
}
myExample
函数中的customer
是一个字符串。如何引用模块级别 customer
?
如果是 class,我可以使用 this.customer
,但 this
在模块中不起作用,Example.customer
也不起作用,除非客户出口...
通常,模块会导出 classes、函数或其他元素(如枚举)。
本例中的export module Example
仅指定Example
实际上是一个命名空间,这意味着任何对myExample函数的引用都必须以命名空间名称为前缀,即Example.myExample()
.
您说除非出口,否则客户不可用,您是对的。这是因为 export module Example
只是指定一个命名空间,而不是导出变量或 classes.
很难推测您为什么使用 export module
,而不是 export class
:
export class Example2 {
customer: string = 'Example2.customer';
myExample(customer: string) {
console.log(`Example ${this.customer}`);
console.log(`Example ${customer}`);
}
}
这个 class 实际上是一个模块,因为使用了 export
关键字。
这是问题 TS 的 JS 生成版本:
define(["require", "exports"], function (require, exports) {
"use strict";
var Example;
(function (Example) {
var customer;
function myExample(customer) {
// How to reference the module level customer object here?
// Is there a standard to make these module level variables Pascal Case to prevent this overlap?
}
Example.myExample = myExample;
})(Example = exports.Example || (exports.Example = {}));
});
由于未导出客户,因此生成为私有。而现在,标准的 Javascript 变量作用域规则生效,没有办法引用模块级客户。最简单和最简单的解决方案(恕我直言,模块级私有变量的标准约定)是强调最外面的客户:
export module Example{
let _customer : any;
export function myExample(customer: string) {
_customer = customer;
}
}
考虑这个模块:
export module Example{
let customer : any;
export function myExample(customer: string) {
// How to reference the module level customer object here?
// Is there a standard to make these module level variables Pascal Case to prevent this overlap?
}
}
myExample
函数中的customer
是一个字符串。如何引用模块级别 customer
?
如果是 class,我可以使用 this.customer
,但 this
在模块中不起作用,Example.customer
也不起作用,除非客户出口...
通常,模块会导出 classes、函数或其他元素(如枚举)。
本例中的export module Example
仅指定Example
实际上是一个命名空间,这意味着任何对myExample函数的引用都必须以命名空间名称为前缀,即Example.myExample()
.
您说除非出口,否则客户不可用,您是对的。这是因为 export module Example
只是指定一个命名空间,而不是导出变量或 classes.
很难推测您为什么使用 export module
,而不是 export class
:
export class Example2 {
customer: string = 'Example2.customer';
myExample(customer: string) {
console.log(`Example ${this.customer}`);
console.log(`Example ${customer}`);
}
}
这个 class 实际上是一个模块,因为使用了 export
关键字。
这是问题 TS 的 JS 生成版本:
define(["require", "exports"], function (require, exports) {
"use strict";
var Example;
(function (Example) {
var customer;
function myExample(customer) {
// How to reference the module level customer object here?
// Is there a standard to make these module level variables Pascal Case to prevent this overlap?
}
Example.myExample = myExample;
})(Example = exports.Example || (exports.Example = {}));
});
由于未导出客户,因此生成为私有。而现在,标准的 Javascript 变量作用域规则生效,没有办法引用模块级客户。最简单和最简单的解决方案(恕我直言,模块级私有变量的标准约定)是强调最外面的客户:
export module Example{
let _customer : any;
export function myExample(customer: string) {
_customer = customer;
}
}