I'm not talking about C# but my question relates to it.
C# syntax:
IList<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
foreach (var item in list) Console.WriteLine(item); // 1, 2, 3
IList
is an interface while List
is a class. And I know that there is no concept interface
in javascript, but I can use class instead.
So, js syntax looks like:
let IList = (function () {
let _T;
class IList {
constructor(T) {
_T = T;
this[_T] = [];
}
Add(item) {
let _this = this;
_this[_T].push(item);
}
*[Symbol.iterator]() {
let _this = this;
let source = _this[_T];
// it doesn't support forEach method
// source.forEach(x => yield x);
for (let s of source) yield s;
}
}
return IList;
}());
let List = (function () {
class List extends IList {
constructor(T) {
super(T);
}
}
return List;
}());
// Usage:
let list = new List('int');
list.Add(1);
list.Add(2);
list.Add(3);
for (let item of list) console.log(item); // 1, 2, 3
I'm stuck with IList
class, I don't want to allow creating an instance of that it.
let list = new IList('int'); // throw some exception here
How can I edit it? Thank you!