Class-Loop
Creates an Iterator for the class, to use in for-of loop
Problem
In Javascript, for-of
loop cannot be used in object.
Eg:
class TestClass {
constructor() {
this.property0 = 'prop0';
this.property1 = 'prop1';
}
}
const instance = new TestClass();
for (let i of instance) {
properties.push(i);
}
Using 'Class-Loop' module
import ClassLoop
class.
import { ClassLoop } from 'class-loop';
Using with a class
import { ClassLoop } from 'class-loop';
class TestClass extends ClassLoop {
property0: string;
property1: string;
constructor() {
super();
this.property0 = 'prop0';
this.property1 = 'prop1';
}
}
const instance = new TestClass();
const properties = [];
for (const i of instance) {
properties.push(i);
}
console.log(properties);
const { ClassLoop } = require('class-loop');
Using with a class
const { ClassLoop } = require('class-loop');
class TestClass extends ClassLoop {
constructor() {
super();
this.property0 = 'prop0';
this.property1 = 'prop1';
}
}
const instance = new TestClass();
const properties = [];
for (let i of instance) {
properties.push(i);
}
console.log(properties);
Override loop function in child class.
import { ClassLoop } from 'class-loop';
class TestClass extends ClassLoop {
property0: string;
property1: string;
constructor() {
super();
this.property0 = 'prop0';
this.property1 = 'prop1';
}
loopForIterator(): any {
let counter = 2;
return {
next: (): any => {
return {
value: 'test' + counter,
done: counter <= -1
};
}
};
}
}
const instance = new TestClass();
const properties = [];
for (let i of instance) {
properties.push(i);
}
console.log(properties);
const { ClassLoop } = require('class-loop');
class TestClass extends ClassLoop {
constructor() {
super();
this.property0 = 'prop0';
this.property1 = 'prop1';
}
loopForIterator(): any {
let counter = 2;
return {
next: (): any => {
return {
value: 'test' + counter,
done: counter <= -1
};
}
};
}
}
const instance = new TestClass();
const properties = [];
for (let i of instance) {
properties.push(i);
}
console.log(properties);