我有以下 3 种不同的方法来创建对象实例。
我想知道其中哪一个更快,占用的内存更少,为什么?
//Option 1
export interface Animal1 {
name: string;
color: string;
isHungry: boolean;
}
let animal: Animal1 = {
name: 'cat',
color: 'brown',
isHungry: true
};
//Option 2
export class Animal2 {
name: string;
color: string;
isHungry: boolean;
}
let animal2: Animal2 = new Animal2();
animal2.name = 'cat';
animal2.color = 'brown';
animal2.isHungry = true;
//Option 3
export class Animal3 {
constructor(public name:string, public color:string, public isHungry:boolean) {}
}
let animal3 = new Animal3('cat', 'brown', true);
Run Code Online (Sandbox Code Playgroud)
就编码风格和维护而言,我更喜欢在单元测试中使用构建器类的选项 1。选项 …
我看到以下语法:
var comparer = Comparer<TItem>.Default;
Run Code Online (Sandbox Code Playgroud)
这种语法如何工作?
我Comparer原本以为必须是新手
我正在对一个包含clarity带有clarit directives.
我已经模拟了清晰度标签,但它们有clrDgItems我无法用指令类模拟的指令。
<clr-dg-row *clrDgItems="let item of items$ | async | filter : listFilter.keyword : ['trackingCode', 'title']" [clrDgItem]="episode">
Run Code Online (Sandbox Code Playgroud)
我可以替换clrDgItems为ngFor,但表过滤器停止工作。
如果没有它,测试将无法编译:
无法绑定到“clrDgItemsOf”,因为它不是“clr-dg-row”的已知属性
当我将mockDirective添加为:我收到错误
失败:模块“DynamicTestModule”声明了意外值“[object Object]”
function MockDirective(options: any): Directive {
const metadata: Directive = {
selector: options.selector,
inputs: options.inputs,
outputs: options.outputs
};
return new Directive(metadata);
}
TestBed.configureTestingModule({
imports: [
RouterTestingModule
],
declarations: [
MockDirective({
selector: '[clrDgItemsOf]',
}),
MockComponent({
selector: 'clr-dg-row',
template: '<ng-content></ng-content>',
outputs: ['clrDgItemsOf'],
inputs: ['clrDgItem']
}), ...
Run Code Online (Sandbox Code Playgroud)
有什么建议么?
是否有任何类型的集合System.Collections不继承自IEnumerable或IEnumerable<T>?
我需要一个例子。
.net ×2
c# ×2
angular ×1
c#-4.0 ×1
clarity ×1
collections ×1
generics ×1
ienumerable ×1
javascript ×1
typescript ×1
unit-testing ×1