有没有办法在我的代码中使用 Typescript.Collections.HashTable?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18751766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to use Typescript.Collections.HashTable in my code?
提问by Sébastien Renault
I see in the code of the Typescript compiler, an implementation of "HashTable" (in the files src/compiler/core/hashTable.ts).
我在 Typescript 编译器的代码中看到了“HashTable”的实现(在文件 src/compiler/core/hashTable.ts 中)。
Do you know is there a way I can use it directly in my Typescript project ?
你知道有什么方法可以直接在我的 Typescript 项目中使用它吗?
回答by Ross Scott
You can implement a very simple hashtable where the key is a string by defining an interface
您可以通过定义一个接口来实现一个非常简单的哈希表,其中的键是一个字符串
class Person {
name: string;
}
interface HashTable<T> {
[key: string]: T;
}
var persons: HashTable<Person> = {};
persons["bob"] = new Person();
var bob = persons["bob"];
It can only be keyed on a string or a number though.
不过,它只能键入字符串或数字。
回答by Ralms
I get socked with people trying to reinvent the wheel.
我对试图重新发明轮子的人感到厌烦。
Typescript is a superset of Javascript, meaning that any Javascript works as well.
Typescript 是 Javascript 的超集,这意味着任何 Javascript 都可以正常工作。
In Javascript you have Map() which is not 100% like an hashtable but has similiar usage. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Keyed_collections
在 Javascript 中,您有 Map(),它不是 100% 像哈希表,但具有类似的用法。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Keyed_collections
One thing to beaware is the fact that the implementation of Map here lets you set the same key multiple times, where it overrides the old value.
需要注意的一件事是,这里 Map 的实现允许您多次设置相同的键,它会覆盖旧值。
One function I've made and use to avoid this is the following:
我为避免这种情况而制作和使用的一个功能如下:
private add<K,V>(map:Map<K,V>, key:K, value:V){
if(map.has(key)){
throw new TypeError("Key "+ key +" already exists!");
}else{
map.set(key,value);
}
}
I pass it a map I define previously by doing something like:
我通过执行以下操作将我之前定义的地图传递给它:
MyMap = new Map();
or
或者
MyMapStrict = new Map<string,string>();
And than pass a key and a value that must respect the map key and value types. Otherwise Typescript compiler will throw an error.
然后传递一个必须尊重映射键和值类型的键和值。否则 Typescript 编译器会抛出错误。
Example:
例子:
add(MyMapStrict, "myKey", "myvalue");
Hope it helps.
希望能帮助到你。
回答by basarat
Download the file "hashTable.ts" and put it right next to your file. Then at the top of your file do:
下载文件“hashTable.ts”并将其放在您的文件旁边。然后在你的文件的顶部做:
///<reference path='hashTable.ts' />
PS: I would recommend having a look at a libTypeScript Generic Collections
I authored. Here is a dictionary sample:
PS:我建议看一下TypeScript Generic Collections
我编写的库。这是一个字典示例:
class Person {
constructor(public name: string, public yearOfBirth: number,public city?:string) {
}
toString() {
return this.name + "-" + this.yearOfBirth; // City is not a part of the key.
}
}
class Car {
constructor(public company: string, public type: string, public year: number) {
}
toString() {
// Short hand. Adds each own property
return collections.toString(this);
}
}
var dict = new collections.Dictionary<Person, Car>();
dict.setValue(new Person("john", 1970,"melbourne"), new Car("honda", "city", 2002));
dict.setValue(new Person("gavin", 1984), new Car("ferrari", "F50", 2006));
console.log("Orig");
console.log(dict);
// Changes the same john, since city is not part of key
dict.setValue(new Person("john", 1970, "sydney"), new Car("honda", "accord", 2006));
// Add a new john
dict.setValue(new Person("john", 1971), new Car("nissan", "micra", 2010));
console.log("Updated");
console.log(dict);
// Showing getting / setting a single car:
console.log("Single Item");
var person = new Person("john", 1970);
console.log("-Person:");
console.log(person);
var car = dict.getValue(person);
console.log("-Car:");
console.log(car.toString());