在打字稿中使用初始化程序 [英] Using initializer in typescript

查看:90
本文介绍了在打字稿中使用初始化程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我上课了



<前lang =C#> export class 选项{

// 主要属性

valueField:any ;
labelField:any;
sortField: string [];
searchFields: string [];
项目:任何[];
currentResults:any [];
分隔符: string ;
模式:SelectizeMode;选择
:任意;
placholder: string ;

get hasOptions(): Boolean {
返回 .items? .items.length > 0 false ;
}


构造函数(_valueField?: string ,_ labelField?: string
_searchFields?: string [],_ item?:any [],_ currentResults?:any [],
_selecetd?:any,_delimiter?: string ,_sortField?: string [],_ mode?:SelectizeMode, _placholder?: string ){


this .valueField = _valueField;
this .labelField = _labelField;
this .sortField = _sortField || [ .valueField];
this .searchFields = _searchFields || [ this .labelField, this .valueField];
.items = _items;
.delimiter = _delimiter || ' ,';
.mode = _mode || SelectizeMode.single;
this .selected = _selecetd;
this .currentResults = _currentResults || [];
.placholder = _placholder || ' select ...';
}
}





i希望在不必传递所有可选参数的情况下设置该类的对象



喜欢c#



例如我想要的那样



选项x =选项(){
分隔符:' ,'
}





我尝试了什么:



i使所有构造函数参数成为可选的



创建接口而不是它工作的类但产生另一个我需要新实例的问题所以我不得不把它变成类,因为不能接受接口的实例

解决方案

没有简单的方法可以完全按照自己的意愿去做。

有一个替代方案可能符合你的需要。



而不是创建一个类,你创建一个所有成员都可选的接口。



  export   interface  IOptions {
valueField?:string;
labelField?:string;
searchFields?:string [];
...
}



然后可以从一个简单的对象文字中完成实例化。

  var 选项:IOptions = {
valueField: aa
labelField: bb
};



它也适用于以下方法:

  var  x =  new  class1({valueField:  AA}); 

class class1 {
valueField:string;
labelField:string;

构造函数(选项:IOptions){
.valueField = options.valueField || empty;
this .labelField = options.labelField || empty;
}
}


hi i have class

export class Options {

    //main properties

    valueField: any;
    labelField:any;
    sortField: string[];
    searchFields: string[];
    items: any[];
    currentResults: any[];
    delimiter: string;
    mode: SelectizeMode;
    selected: any;
    placholder: string;

    get hasOptions(): Boolean {
        return this.items ?  this.items.length > 0 : false;
    }


    constructor(_valueField?: string, _labelField?: string,
        _searchFields?: string[], _items?: any[], _currentResults?: any[],
        _selecetd?: any, _delimiter?: string, _sortField?: string[], _mode?: SelectizeMode, _placholder?: string) {


        this.valueField = _valueField;
        this.labelField = _labelField;
        this.sortField = _sortField || [this.valueField];
        this.searchFields = _searchFields || [this.labelField, this.valueField];
        this.items = _items;
        this.delimiter = _delimiter || ',';
        this.mode = _mode || SelectizeMode.single;
        this.selected = _selecetd;
        this.currentResults = _currentResults || [];
        this.placholder = _placholder || 'select...';
    }
}



i want to instate object of that class without having to pass all optional paramters

like c#

for example i want to like that

Options x =     Options(){
delimiter:','
}



What I have tried:

i made all constructor parameters as optional

create Interface instead of class it worked but produce another problem that i need new Instances so i had to make it class as can not take instance of interface

解决方案

There is no easy way to do exactly what you want.
There is an alternative though which might fit your need.

Rather than creating a class you create an interface with all members optional.

export interface IOptions { 
 	valueField?: string; 
	labelField?: string;
	searchFields?: string[];
	...
}


Then the instantiation can be done from a simple object literal.

var options : IOptions =  {
	valueField: "aa",
	labelField: "bb"
};


It also works in methods:

var x = new class1({ valueField:"AA"});

class class1 {
 	valueField: string; 
	labelField: string;
  
	constructor(options: IOptions){
		this.valueField = options.valueField || "empty";	  
		this.labelField = options.labelField || "empty";	  
	}
}


这篇关于在打字稿中使用初始化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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