黑客如果你正在嘗試的功能添加到該集合原型,或添加polyfills設置的那一刻,你可以做到以下幾點:
declare global {
interface Set<T> {
// polyfill
isSuperset(subset: Set<T>) : boolean;
// new function
someNewFunc(): boolean;
}
}
// add polyfill to the Set prototype as mentioned in the doc you provided: https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Set
Set.prototype.isSuperset = function(subset) {
for (var elem of subset) {
if (!this.has(elem)) {
return false;
}
}
return true;
}
//add the new someNewFunc;
Set.prototype.someNewFunc = function() {
// some logic here...
return true;
}
使用:
stringSet = new Set<string>()
stringSet.isSuperset(someOtherSet);
stringSet.someNewFunc(); // returns true
不錯,謝謝。只有一個問題更多,我怎樣才能將它從應用程序組件分離到另一個文件。我正在嘗試,但似乎只有界面正在導出 –
這是香草js,必須包括在蒸騰後生成的js文件中。你可以通過webpack或任何你正在使用的綁定器來包含它。偶然使用角度cli? –
之後?但後來我無法利用vscode intellisense –