This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function * generator() { | |
yield 1; | |
return 2; | |
yield 3; // we will never reach this yield | |
} | |
const gen = generator(); | |
gen.next(); // {value: 1, done: false} | |
gen.next(); // {value: 2, done: true} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function * generator(arr) { | |
for (const el in arr) | |
yield el; | |
} | |
const gen = generator([0, 1, 2]); | |
for (const g of gen) { | |
console.log(g); // 0 -> 1 -> 2 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function * generator() { | |
yield 1; | |
} | |
generator.prototype.__proto__; // Generator {constructor: GeneratorFunction, next: ƒ, return: ƒ, throw: ƒ, Symbol(Symbol.toStringTag): "Generator"} | |
// as Generator is not global variable we have to write something like this | |
generator.prototype.__proto__.math = function(e = 0) { | |
return e * Math.PI; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Notification.requestPermission((permission) => { | |
switch (permission) { | |
case 'granted': { | |
console.log('Now we can send notifications!'); | |
break; | |
} | |
case 'denied': { | |
console.log('User close the request pop-up!') | |
} | |
case 'default': { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (!('Notification' in window)) { | |
// Place notification logic here | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Notification | |
.requestPermission() | |
.then((permission) => { | |
if (permission !== 'granted') throw new Error('No access!'); | |
}) | |
.then(() => { | |
console.log('We have an access!'); | |
// Handle granted permission here | |
}) | |
.catch((err) => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const title = 'I\'m notification!'; | |
notification(title); // wrong - notification is not defined | |
Notification(title); // wrong - Failed to construct Notification | |
new Notification(title); // OK | |
const notification = new Notification(title); // OK |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const notificationTitle = 'I\'m notification'; // string | |
const notificationOptions = { | |
tag: 'notification', // tag for notification | |
renotify: true, // defalt false, renotifying after old notification | |
body: 'Here is my desription', // description under title | |
icon: 'https://avatars1.githubusercontent.com/u/22643415', // image URL | |
vibrate: [50, 100, 150] // vibration on notification | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const notificationProps = { | |
title: 'I\'m notification', | |
body: 'Here is my desription', | |
icon: 'https://avatars1.githubusercontent.com/u/22643415', | |
vibrate: [50, 100, 150] | |
} | |
const myNotification = new Notification(notificationProps.title, notificationProps); | |
function closeNotification(notifyID, time) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
switch (Notification.permission) { | |
case 'granted': { | |
new Notification('Hello world!'); | |
break; | |
} | |
case 'denied': { | |
alert('No notification for you!'); | |
break; | |
} | |
case 'default': { |