[英]How can I add a user keyword to the VS2013 TypeScript language text editor?
I need to add "self" as a "user keyword" but I cannot see to do this in VS2013. 我需要将“self”添加为“用户关键字”,但我无法在VS2013中看到这样做。 I would like it to be a user keyword for the TypeScript editor window.
我希望它是TypeScript编辑器窗口的用户关键字。
I am making frequent use of "self" in my code. 我在我的代码中经常使用“self”。 Is there a way that I can add the word "self."
有没有办法可以添加“自我”这个词。 as a keyword and have it appear in a different color in my TypeScript text editing window?
作为关键字,它在我的TypeScript文本编辑窗口中以不同的颜色显示?
Here's an example of where I would use self and would like to have it highlighted: 这是我将使用self并希望突出显示的示例:
class X {
abc=1;
static $inject = ['$http'];
constructor(private $http: ng.IHttpService) {}
doTask = () => {
var self = this;
this.$http({
xx
})
.success((data) => {
self.abc = data
})
}
}
Update: 更新:
I have added a few lines to the question to more closely match the way I am using self
. 我在这个问题上添加了几行,以便更贴近我使用
self
。
This will work fine with a lambda : 这将适用于lambda :
class X {
doTask = () => {
this.doJob({
foo: "bar"
}).success((data) => {
this.abc = data;
});
}
}
It compiles to: 它编译为:
function X() {
var _this = this;
this.doTask = function () {
_this.doJob({
foo: "bar"
}).success(function (data) {
_this.abc = data;
});
};
}
I recommend trying ReSharper ( http://www.jetbrains.com/resharper/ ) to extend the syntax highlighting options in VS2013. 我建议尝试使用ReSharper( http://www.jetbrains.com/resharper/ )来扩展VS2013中的语法高亮选项。 It's a 30-day trial, but if it does what you need then I'd say it's worth purchasing.
这是一个30天的试用期,但如果它能满足您的需求,那么我会说它值得购买。 Here's a link to the documentation for the extended color highlighting:
以下是扩展颜色突出显示文档的链接:
http://www.jetbrains.com/resharper/webhelp/Coding_Assistance__Syntax_Highlighting.html http://www.jetbrains.com/resharper/webhelp/Coding_Assistance__Syntax_Highlighting.html
From your question, it seems that all you're interested in doing is specifying another keyword to highlight so that it visually stands out in the code. 从您的问题来看,您似乎只想指定另一个要突出显示的关键字,以便在代码中直观地突出显示。 ReSharper will allow you to easily do this.
ReSharper将允许您轻松完成此操作。
This is how would your class look like property written in TypeScript using methods. 这就是你的类看起来像使用方法用TypeScript编写的属性。 Then you would not need to bother with
self
. 然后,你就不会需要费心
self
。
class X {
private abc: number = 1;
public doTask(): void {
this.doJob()
.success((data) => this.processData(data));
}
private processData(data: any): void {
this.abc = data.id;
}
}
You can modify your code example to take better advantage of TypeScript and avoid the need to define a self
variable. 您可以修改代码示例以更好地利用TypeScript,并避免定义
self
变量。 Consider the following. 考虑以下。
class X {
abc = 1;
static $inject = ['$http'];
constructor(private $http: ng.IHttpService) { }
doTask() {
this.$http({
xx
})
.success(data => {
this.abc = <number>data;
});
}
}
The body of doTask
is defined in a function rather than a lambda. doTask
的主体是在函数而不是lambda中定义的。 When TypeScript sees a function (named or anonymous), it uses it as the scope that inner this
references should apply to. 当TypeScript看到一个函数(命名或匿名)时,它会将其用作
this
引用应该应用于的内部范围。 TypeScript implements this by creating a _this
variable in the .js file at the function level, which mirrors the self
variable you were looking to create by hand. TypeScript通过在函数级别的.js文件中创建一个
_this
变量来实现这一点,它反映了您想要手动创建的self
变量。
Just as it is important to use a function at the outer scope, it is important to use a lambda for the success handler. 正如在外部范围内使用函数很重要一样,将lambda用于成功处理程序也很重要。 If you had coded the handler like the following, it would create a new "this scope", causing
_this
to not be generated. 如果您已经像下面那样对处理程序进行了编码,那么它将创建一个新的“此范围”,导致
_this
不会生成。
.success(function (data) {
this.abc = <number>data; // Not desired behavior - creates a new abc variable
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.