简体   繁体   English

我想为使用TypeScript创建的所有对象或引用创建扩展方法

[英]I want to create an extension method to all object or reference that are created using TypeScript

Actually, I am currently thinking of creating an extension method to all the object or reference that are created in my component 实际上,我目前正在考虑为在组件中创建的所有对象或引用创建扩展方法。

I know how to create an extension method in typeScript but I am not able to get what I want to use an interface to get the behavior I wanted. 我知道如何在typeScript中创建扩展方法,但是我无法获得要使用接口来获得想要的行为的东西。

I tried using the Object in the interface but it won't work 我尝试在界面中使用对象,但无法正常工作

MyExtension Method : extension.model.ts MyExtension方法:extension.model.ts

declare global {
  interface Object {
    thousandsSeperator(): String;
  }
}

Object.prototype.thousandsSeperator = function(): string {
  return Number(this).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}
export {};
// this is just a simple extension method

Now, whenever if someone tries to create an object of class or reference of an interface then, they should get the extension method I created. 现在,只要有人尝试创建类的对象或接口的引用,他们就应该获得我创建的扩展方法。

// Object
Object1 = new MyComponentClass();

// Reference
Object2: MyComponentClass;

this.Object1.thousandsSeperator();

this.Object2.thousandsSeperator();

fooObj in the below example is assigned to Foo object. 下例中的fooObj分配给Foo对象。 so all the methods in the object and the inherited properties can be accessed. 因此可以访问对象中的所有方法和继承的属性。

But

fooObj1 is just declared as Foo type. fooObj1只是声明为Foo类型。 ie, we are declaring that the fooObj1 can only have Object of Foo type. 即,我们声明fooObj1只能具有Foo类型的Object。

// Add thousandsSeperator in the Object interface.
interface Object {
    thousandsSeperator(): String;
}

// assign the method to the object prototype.
Object.prototype.thousandsSeperator = function(): string {
    // this is the instance of the object and value is the property on it.
    return Number(this.value).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
};
// now all the object of the created will get the thousandsSeperator method.
class Foo {
    value: string = '123456';
}
// create an object 
let fooObj: Foo = new Foo();
// here on the fooObj thousandsSeperator method can be accessed.
fooObj.thousandsSeperator();

// undeclared variable.
let fooObj1: Foo 
// this is eqivalent to var fooObj1; 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在组件方法中创建的对象的TypeScript测试属性 - TypeScript test properties of object created within a component's method 我想在打字稿和Firebase上进行计算时未定义的对象 - Undefined object when I want to calculate on typescript and firebase 在Angular 2和Typescript中创建对Date的扩展 - Create extension to Date in Angular 2 and Typescript javascript typescript 创建一个 object - javascript typescript create a object Angular / Typescript 使用方法中的参数值作为 js 对象值 - Angular / Typescript using the parameter value from method as a js object value 在扩展方法TypeScript中创建数组 - Creating Array inside extension method TypeScript 我在Angular v8.1.3中创建了My angular项目,我想运行上面所有的angular版本 - I created My angular project in Angular v8.1.3 , I want to run all the above angular version 当有人选择一个文件时,我想动态创建视频hmtl,然后以角度2和打字稿形式显示具有播放功能的视频 - when someone selects a file I want to dynamically create the video hmtl then show the video with play features in angular 2 and typescript 我想从 TypeScript 中的数据数组对象访问内部数组数据 - I want to access inner array data from array object of data in TypeScript 为什么 ngfor 指令不起作用,尽管我在 Typescript class 中创建了正确的 object - Why ngfor directive is not working, though I have created proper object in Typescript class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM