在TypeScript中扩展基本类型,错误:“_这未定义...” [英] Extend Basic Types in TypeScript, Error: "_this is not defined..."

查看:1685
本文介绍了在TypeScript中扩展基本类型,错误:“_这未定义...”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在TypeScript中重写一些JavaScript代码。其中一些代码引用了我添加到字符串对象原型的扩展。

  String。 prototype.format = function(){
var formatted = this;
for(var i = 0; i< arguments.length; i ++){
formatted = formatted.replace(
RegExp(\\ {+ i +\ \},'g'),arguments [i] .toString());
}
返回格式化;
};

然而,使用类型脚本添加此内容非常具有挑战性。



我见过一些例子,你声明一个基本接口的扩展,然后为原型分配一个函数来匹配接口并提供你的功能。像这样......

  interface String {
showString :()=>串;
}

String.prototype.showString =():string {
返回此;
};

除了这个错误,因为_这是未定义的......



我尝试的其他事情是创建一个新类来扩展字符串...

 导出类MoreString扩展字符串{

}

但是这也行不通,因为你只能扩展类,而string / String不是类,而是内置类型。



扩展String和访问我的最简单方法是什么扩展方法?

解决方案

我在当天晚些时候遇到了另一个问题,让我看到了这里发生的事情。从顶部开始,这里是...



TypeScript建立在JavaScript之上,所以像@Nypan所说的JavaScript是有效的TypeScript。因此,这些差异很容易被忽视。



像这样的JavaScript函数引用了函数用this执行的范围。

  var f = function(postFix){return this + postFix}; 

要添加TypeScript语法,您需要定义类型



< pre class =lang-ts prettyprint-override> var f = function(postFix:string):string {return this + postFix};

在这两种情况下,这就像经典JavaScript一样引用了函数的范围。但是,当我们这样做时情况发生了变化...

  var f =(postFix:string) :string {return this + postFix}; 
//或更正确
var f =(postFix:string):string => {return this + postFix};

当您从参数前面删除该功能时,它不再是经典功能。它变成了胖箭功能,显然即使没有使用=>语法。在上面的例子中,this现在指的是函数存在于C#中的类。



在我尝试将函数分配给字符串的原型时我省略了function关键字,因此它被解释为Fat Arrow函数,并尝试将其绑定到类的范围。但是函数不存在于类中并导致错误_this not defined。



当我添加function关键字时,该函数被解释为I打算正常工作。

  interface String {
format :()=>串;
}

String.prototype.format = function():string {
var formatted = this;
for(var i = 0; i< arguments.length; i ++){
formatted = formatted.replace(
RegExp(\\ {+ i +\ \},'g'),arguments [i] .toString());
}
返回格式化;
};


I am trying to rewrite some of my JavaScript code in TypeScript. Some of this code has references to an extension I added to the string object prototype.

String.prototype.format = function () {
    var formatted = this;
    for (var i = 0; i < arguments.length; i++) {
        formatted = formatted.replace(
            RegExp("\\{" + i + "\\}", 'g'), arguments[i].toString());
    }
    return formatted;
};

However adding this with type script has been quite challenging.

I have seen examples where you declare an extension of a basic interface then assign an function to the prototype to match the interface and provide your functionality. Like so...

interface String {
    showString: () => string;
}

String.prototype.showString = (): string {
    return this;
};

Except this errors because "_this is not defined..."

The other things I have tried is to create a new class to extend string...

export class MoreString extends string {

}

However this also does not work because you can only extend classes and string/String are not classes but built in types.

What is the simplest way to extend String and access my extension method?

解决方案

I ended up running into another issue later in the day that made me see what was happening here. From the top, here it is...

TypeScript is built on top of JavaScript, so like @Nypan says JavaScript is valid TypeScript. Because of this the differences are very easy to overlook.

A JavaScript function like this references the scope that the function executes in with "this".

var f = function (postFix) {return this + postFix};

To add TypeScript syntax you would define types

var f = function (postFix: string): string {return this + postFix};

In both of these cases this refers to the scope of the function just like classic JavaScript. However, things change when we do this...

var f = (postFix: string): string {return this + postFix};
//or more correctly
var f = (postFix: string): string => {return this + postFix};

When you remove the function from in front of the parameters then it is no longer a classic function. It becomes a "Fat Arrow" function, apparently even with out using the "=>" syntax. In the examples above "this" now refers to the class that the function exists in like in C#.

In my attempt to assign a function to the prototype of string I omitted the function keyword so it was interpreted as a "Fat Arrow" function and tries to bind this to the scope of the class. However the function dose not exist in a class and causes the error "_this is not defined".

When I add the "function" keyword, the function is interpreted as I intended and works correctly.

interface String {
    format: () => string;
}

String.prototype.format = function () : string {
    var formatted = this;
    for (var i = 0; i < arguments.length; i++) {
        formatted = formatted.replace(
            RegExp("\\{" + i + "\\}", 'g'), arguments[i].toString());
    }
    return formatted;
};

这篇关于在TypeScript中扩展基本类型,错误:“_这未定义...”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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