- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设您有一个装饰器来跟踪它装饰了哪些字段:
interface FieldTracker {
fields: string[];
}
const decorator = <T extends FieldTracker>(target: T, fieldName: string) => {
target.fields = target.fields || [];
target.fields.push(fieldName);
};
然后假设您创建了一个使用该装饰器的抽象 Base
类:
abstract class Base implements FieldTracker {
fields: string[];
@decorator
a: string = 'a';
}
然后您创建两个扩展 Base
类的类。
class FirstClass extends Base {
@decorator
b: string = 'b';
}
class SecondClass extends Base {
@decorator
c: string = 'c';
}
在实例化 SecondClass
时,它的 fields
将包含在 FirstClass
中修饰的字段:
const secondInstance = new SecondClass();
expect(secondInstance.fields).toEqual(['a', 'c']);
这导致:
Expected value to equal:
["a", "c"]
Received:
["a", "b", "c"]
如果我将参数记录到 decorator
,我得到:
target: Base {}, fieldName: 'a'
target: FirstClass {}, fieldName: 'b'
目标:SecondClass {},fieldName:'c'
请注意,Base
是抽象的
,无法实例化。 target
如何成为它的一个实例?
请注意,我什至从未实例化过 FirstClass
。 target
如何成为它的一个实例?
如果我不在 Base
上使用 decorator
,则不会发生这种情况。这意味着 fields
在 Base.prototype
上,这似乎不应该存在。
最佳答案
这里有点困惑。
Base
类的抽象
性质意味着,如果您尝试直接构造 Base< 的实例,编译器将会对您大喊大叫
与它。它仍然具有类构造器的所有功能,包括拥有一个原型(prototype)
。如果您在 the TypeScript Playground 检查您的代码发出的 JavaScript,您可以看到这一点。 .
装饰器作用于每个类构造函数的prototype
(所以当你装饰Base
时,它正在修改Base.prototype
).它不(直接)作用于该类的任何实例。对于您装饰的每个类,装饰器只被调用一次。
子类的原型(prototype)
inherits from父类(super class)的 prototype
。这样,子类实例的原型(prototype)链包括子类构造函数的 prototype
以及父类(super class)构造函数的 prototype
。
如果您将一个数组赋值给一个变量,您并不是在复制数组的内容;只有一个数组对象,您从一个变量对其所做的任何更改都将在另一个变量中可见。
话虽如此,让我们检查一下您的装饰器:
const decorator = <T extends FieldTracker>(target: T, fieldName: string) => {
target.fields = target.fields || []; // hmm
target.fields.push(fieldName);
};
在标记为 //hmm
的行中,您正在检查传入的 prototype
对象的 fields
属性。对于 Base.prototype
,这将不存在并将被初始化为一个新的空数组。对于 FirstClass.prototype
,这不会被直接找到,但由于 FirstClass.prototype
继承自 Base.prototype
,它会在那里找到。通过将 FirstClass.prototype.fields
设置为 Base.prototype.fields
,您将属性直接添加到 FirstClass.prototype
,但值是与 Base.prototype
相同的数组对象。当您将值推送到 FirstClass.prototype.fields
时,您也会在 Base.prototype.fields
上看到更改。对于 SecondClass.prototype.fields
也是类似的。
这意味着你得到了不良行为:
console.log(Base.prototype.fields); // Array [ "a", "b", "c" ]
console.log(FirstClass.prototype.fields); // Array [ "a", "b", "c" ]
console.log(SecondClass.prototype.fields); // Array [ "a", "b", "c" ]
解决这个问题很简单;不要复制数组引用,而是将其内容复制到一个新数组。最简单的方法是使用原始数组的 slice()
method :
const decorator = <T extends FieldTracker>(target: T, fieldName: string) => {
target.fields = (target.fields || []).slice(); // all better
target.fields.push(fieldName);
};
现在,如果您运行上面的代码,测试应该会通过。具体来说,您应该看到:
console.log(Base.prototype.fields); // Array [ "a" ]
console.log(FirstClass.prototype.fields); // Array [ "a", "b" ]
console.log(SecondClass.prototype.fields); // Array [ "a", "c" ]
希望对您有所帮助;祝你好运!
关于typescript - 为什么基类上的 TypeScript 装饰器会导致扩展相互影响?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48122319/
这个问题已经有答案了: Resolve build errors due to circular dependency amongst classes (12 个回答) 已关闭 3 个月前。 如何允许
让 2 个 Swing 部件做同样的工作是很常见的。例如,我们可以在工具栏中使用一个 button 作为“保存”按钮,而 JMenuItem (文件保存...)也可以做同样的事情。 我的问题是: 有没
我使用 fragment 已经有一段时间了,但我经常遇到一个让我烦恼的问题。 fragment 有时会相互吸引。现在,我设法为此隔离了一个用例,它是这样的: Add fragment A(也使用 ad
我正在使用具有相互 SSL 安全性的 WCF 服务,我想检查一下我对何时使用什么证书的理解。 这是正确的吗? 客户端将客户端公共(public)证书交给服务器 服务器将服务器公共(public)证书交
假设您有一个相互 SSL 服务,除了 SSL 之外,它还有应用程序身份验证。因此,客户端提供证书(以及服务器),但客户端请求(例如 REST 请求)还包含后端应用程序服务器用于验证的用户名/密码。 就
有人让 Android uiautomator 可以同时在多个设备上运行,但做不同的事情吗? 我的意思是,我希望我的测试同时启动设备和应用程序,然后设备 A 执行设备 B 必须使用react的操作。这
我目前正在尝试在客户端和服务器之间实现双向 TLS 身份验证。我遇到了一个 SSL 错误,它的描述性不强。 StackOverflow 也没有太多与之相关的问题,因为大多数时候它是互联网上的单向 TL
这里是新手。我正在做我的第一个元素,我想为不同的人(普通人、 worker 、农民等)提供 slider ,但我不知道如何放置多个 slider 以及如何让它们全部工作。我从 W3schools 获取
我创建了一张翻转卡片,但卡片内的所有 div 似乎都浮在彼此之上。我希望 div 彼此相邻。 我看了很多问题,但似乎找不到答案。我尝试了多种显示:内联;职位:相对;向左飘浮;清除:两者;但我似乎无法让
我正在使用此控件来安排时间。我有一个单选按钮列表,然后是多个内容 Pane 。根据内容,我想在正确的控件中淡入淡出。但出于某种原因,在 div 上放置一个 float 并设置 z-index 并不能使
有什么方法可以解密双向 SSL(客户端和服务器,两种方式)? 我找到了这个链接:https://www.wireshark.org/lists/wireshark-users/201001/msg00
我正在开发一个 Web 应用程序,安全性是我们在此应用程序中的主要关注点之一。我正在查看不同的 API 安全方法(在 OWASP 中提到),无法理解相互 SSL 身份验证和基于 token 的身份验证
我正在尝试使用分配给 kube-dns 服务的集群 IP 从 dnstools pod ping kube-dns 服务。 ping 请求超时。在同一个 dnstools pod 中,我尝试使用暴露的
过去几天我一直在研究这个问题,但我一无所获。 场景是: 现场的 iOS 应用程序将调用我的 REST 服务 (.NET)。我的 REST 服务将使用相互 SSL 握手调用 Apache Web 服务。
我正在尝试向 java swing 应用程序添加 3 个 JSlider,以便三个 slider 的总值(value)总和为 100。每个 slider 都是一个概率, slider A 是将值添加到
我们正在使用 java 客户端(openJDK 1.8.0) 调用需要相互身份验证的 api。为此,我们使用 Java 标准 JKS 文件作为 keystore 和信任库(包含信任证书和身份证书/私钥
有人告诉我使用双向身份验证连接到客户的服务器。服务器身份验证工作顺利,但我们在获取客户端身份验证方面遇到了巨大的麻烦。让我试着解释一下我们的麻烦。 前段时间我公司在 GeoTrust 购买了一个证书,
正在试用 PAW 并且非常喜欢它。我唯一无法正常工作的是使用 HTTPS 相互身份验证。我需要与之交互的一些 API 需要相互验证的 https。 如何告诉 PAW 使用证书进行身份验证?该证书已经在
我们有一个在 Jboss EAP 5.1 中部署并使用 Spring 2.5 已经运行了一年多的 CXF webservice 我们现有的客户证书管理策略如下: 对于非 PROD,证书名为“NAME-
我正在创建一个将调用 API 的 Windows 服务。对于这个过程,我正在尝试建立相互(双向)SSL 身份验证。因为我是新手。我尝试实现一个简单的客户端和服务器项目,它们将相互进行身份验证。 我已经
我是一名优秀的程序员,十分优秀!