Ivá*_*hez 5 javascript ecmascript-5 typescript angular
我正在尝试对 Set 对象实现一些基本操作,如下所示
这是代码
export class Conjunto extends Set<any>{
constructor(initialValues?) {
super();
return new Conjunto(initialValues);
}
isSuperset(subset) {
//some code
}
}
Run Code Online (Sandbox Code Playgroud)
你们有什么想法让它发挥作用吗?或者我做错了什么?
目前我正在使用这个人在这里找到的黑客
如果您尝试向 Set 原型添加函数,或者向 Set 添加填充,您可以执行以下操作:
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;
}
Run Code Online (Sandbox Code Playgroud)
使用:
stringSet = new Set<string>()
stringSet.isSuperset(someOtherSet);
stringSet.someNewFunc(); // returns true
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1988 次 |
| 最近记录: |