简体   繁体   English

访问打字稿属性的获取器和设置器

[英]Access the getter and setter of a typescript property

I have a question about typescript properties: Is it possible to get the setter and getter of a typescript property or to declare a function argument to be of a property of X type? 我有一个关于打字稿属性的问题:是否可以获取打字稿属性的setter和getter或声明一个函数参数为X类型的属性?

The reason is to get some sort of "reference" to a variable which is not possible in plain JS without writing getter/setter wrappers or access the variable via parent object itself (obj["varname"]). 原因是要获得对某种变量的“引用”,这在普通JS中是不可能的,而无需编写getter / setter包装器或通过父对象本身(obj [“ varname”])访问该变量。

For example (with some working code and other parts speculative): 例如(带有一些工作代码和其他推测的部分):

//A sample class with a property
class DataClass<T> {
    private T val;

    public get value(): T {
        return this.val;
    }

    public set value(value: T) {
        this.val = value;
    }
}

//Different ways of modifing a member "by reference"
class ModifyRef {
    public static void DoSomethingByGetterAndSetter(getter: () => string, setter: (val: string) => void) {
        var oldValue = getter();
        setter("new value by DoSomethingByGetterAndSetter");
    }

    public static void DoSomethingByObject(obj: Object, name: string) {
        var oldValue = obj[name];
        obj[name] = "new value by DoSomethingByObject";
    }

    //Is something like this possible?
    public static void DoSomethingByProperty(somePropery: property<string>) {
        var oldVlaue = someProperty;
        someProperty = "new value by DoSomethingByProperty";
    }
}

var inst = new DataClass<string>();

//Calling the DoSomethingByProperty if possible
ModifyRef.DoSomethingByProperty(inst.value);

//Or if not is something like this possible
ModifyRef.DoSomethingByGetterAndSetter(inst.value.get, inst.value.set);

The simplest way to do this would be to provide methods, rather than a property: 最简单的方法是提供方法而不是属性:

//A sample class with a property
class DataClass<T> {
    private val: T;

    public getValue(): T {
        return this.val;
    }

    public setValue(value: T) {
        this.val = value;
    }
}

class ModifyRef {
    public static DoSomethingByGetterAndSetter(getter: () => string, setter: (val: string) => void) {
        var oldValue = getter();
        setter("new value by DoSomethingByGetterAndSetter");
    }
}

var inst = new DataClass<string>();

//Or if not is something like this possible
ModifyRef.DoSomethingByGetterAndSetter(inst.getValue, inst.setValue);

I've long found it very surprising that languages with properties don't include a convenient way to make a reference to a property, and have daydreamed about having this feature in C# . 长期以来,我一直感到非常惊奇的是,具有属性的语言不包含引用属性的便捷方法,并且幻想着在C#中具有此功能 It ought to work on local variables as well. 它也应该对局部变量起作用。

A popular pattern for this kind of first-class or reified property is a single function that can be called in two ways: 这种一流的或经过修饰的属性的一种流行模式是可以通过两种方式调用的单个函数:

  1. no arguments: returns current value. 无参数:返回当前值。
  2. one argument: sets value, returns undefined . 一个参数:设置值,返回undefined

Or in TypeScript terms: 或用TypeScript术语:

interface Property<T> {
    (): T;
    (newVal: T): void;
}

The methods of jQuery objects often work like this. jQuery对象的方法通常是这样工作的。 An example of this pattern in modelling pure data is in Knockout , in which such properties also support change subscriptions, and there's a rather elegant pattern for defining computed properties that automatically recompute when their dependencies change. 在纯数据建模中,此模式的一个示例是Knockout ,其中此类属性还支持更改订阅,并且存在一种相当优雅的模式,用于定义计算属性,当其依赖项发生更改时,这些属性将自动重新计算。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM