简体   繁体   English

如何将用户关键字添加到VS2013 TypeScript语言文本编辑器中?

[英]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.

相关问题 如何将一个VS2013项目中的通用打字稿代码共享到一个单独的VS2013项目中 - How do I share common typescript code from one VS2013 project into a separate VS2013 project 为什么javascript“ class”关键字在VS2013中显示为错误 - Why does the javascript “class” keyword display as a error in VS2013 包含TypeScript的解决方案始终使用VS2013进行永久加载 - Solutions containing TypeScript keep loading eternally, using VS2013 javascript中的VS2013 intellisense - VS2013 intellisense in javascript 如何在Angular Typescript中访问此关键字 - How can I access this keyword in Angular Typescript 如何在WS2013中为WSH(JS / VBS)获取自动完成和Intellisense - How to get Autocomplete and Intellisense in VS2013 for WSH (JS/VBS) 打字稿-在VS2013中运行解决方案时,“ SourceMap文件读取失败” - Typescript - “SourceMap file read failed” when running solution in VS2013 带有VS2013 Update 2 CTP的Typescript 1.0:我们代码中的许多重大更改 - Typescript 1.0 with VS2013 Update 2 CTP: lots of breaking changes in our code VS2013中用于MicroTemplating的语法突出显示 - Syntax Highlighting in VS2013 for MicroTemplating PageMethods无法与VS2013 Web应用程序一起使用 - PageMethods Not Working with VS2013 web application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM