MENU
Generator and async Methods
Generators and async functions may be defined as the methods of a class.
RESETRUNFULL
<!DOCTYPE html><html><body><script>
//(continues from the example in 8.6.2)class GermanShepherd extends Dog{ // default contructor
*bark(){ // generator
while (true){
console.log('whoaaaf');
yield;
console.log('whoaaaf whoaaf');
yield;
console.log('whoaaaf whoaaf whoaf');
yield;
}
}
async sleep(){ // async function
await new Promise(resolve=>{
console.log(this.name+' has fallen asleep.');
resolve();
}).then(()=>
console.log(this.name+' seems to have awaken.'));
console.log('When will '+this.name+' sleep again?');
}}var s = new GermanShepherd('Smartie',3,'Black');var sb = s.bark();sb.next(); // whoaaafsb.next(); // whoaaaf whoaafs.sleep(); // Smartie has fallen asleep.sb.next(); // whoaaaf whoaaf whoafsb.next(); // whoaaaf// Smartie seems to have awaken.// When will Smartie sleep again?
</script></body><html>
In any case, you can omit super() in your subclass, if you omit the constructor altogether in your subclass. A 'hidden' default constructor will be included automatically in your subclass. However, if you do include the constructor in your subclass, super() mustmust must be called in that constructor.For a base class, the default constructor is simple:
constructor(){} |
For a derived class, the default constructor is:
RESETRUNFULL
<!DOCTYPE html><html><body><script>
constructor(...args) {
super(...args);}
</script></body><html>