This file contains hidden or 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
| var func = { | |
| f1: function() { | |
| console.log('f1', this); | |
| var sub = function () { | |
| console.log('sub', this); | |
| }; | |
| sub(); | |
| }, | |
| f2: () => { | |
| console.log('f2', this); |
This file contains hidden or 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 f(a) { | |
| return function(b) { | |
| console.log(a, b); | |
| } | |
| } | |
| var func = f('123'); | |
| func('321'); // 123 321 |
This file contains hidden or 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 a = () => ( | |
| new Promise((res) => { | |
| console.log('-- a start'); | |
| setTimeout(() => { | |
| console.log('a'); | |
| res('-- a finish'); | |
| }, 1000) | |
| }) | |
| ) | |
| const b = () => ( |
This file contains hidden or 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
| var now = new Date(); // Date object | |
| now.toDateString() // "Sun Jul 17 2016" | |
| now.toLocaleDateString() // "2016/7/17" | |
| now.toGMTString() // "Sun, 17 Jul 2016 03:16:49 GMT" | |
| now.toISOString() // "2016-07-17T03:16:49.141Z" | |
| now.toUTCString() // "Sun, 17 Jul 2016 03:16:49 GMT" | |
| now.toLocaleTimeString() // "上午11:16:49" | |
| now.toLocaleString() // "2016/7/17 上午11:16:49" | |
| now.toString() // "Sun Jul 17 2016 11:16:49 GMT+0800 (台北標準時間)" | |
| now.toTimeString() // "11:16:49 GMT+0800 (台北標準時間)" |
This file contains hidden or 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 counter() { | |
| let count = 1; | |
| return function getCount() { | |
| count++; | |
| return count; | |
| } | |
| } | |
| const getCountA = counter(); |
This file contains hidden or 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
| navigator.serviceWorker | |
| .register("/sw.js") | |
| .then((reg) => { | |
| // no controller exist, page wasn't loaded via a service worker | |
| if (!navigator.serviceWorker.controller) { | |
| return; | |
| } | |
| if (reg.waiting) { | |
| // If we have a new version of the service worker is waiting, |
This file contains hidden or 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 urlsToCache = ["/faq", "/contact"]; | |
| self.addEventListener("install", (event) => { | |
| event.waitUntil( | |
| caches.open(CACHE_NAME).then((cache) => { | |
| return cache.addAll(urlsToCache); | |
| }) | |
| ); | |
| }); |
This file contains hidden or 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
| self.addEventListener("fetch", (event) => { | |
| // hijacking path and return a mock HTML content | |
| if (event.request.url.includes("/faq")) { | |
| event.respondWith( | |
| new Response("<div>Mock FAQ Page</div>", { | |
| headers: { "Content-Type": "text/html" }, | |
| }) | |
| ); | |
| } | |
| // hijacking API request and return mock response in JSON format |
This file contains hidden or 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
| // send message to service worker | |
| function handleClickEven() { | |
| worker.postMessage({ action: "skipWaiting" }); | |
| } | |
| // receive message | |
| self.addEventListener("message", (event) => { | |
| if (event.data.action === "skipWaiting") { | |
| // skip waiting to apply the new version of service worker | |
| self.skipWaiting(); |
This file contains hidden or 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
| self.addEventListener("updatefound", () => { | |
| if (reg.installing) { | |
| reg.installing.addEventListener("statechange", () => { | |
| if (worker.state == "installed") { | |
| // display a message to tell our users that | |
| // there's a new service worker is installed | |
| } | |
| }); | |
| } | |
| }); |
OlderNewer