If you’ve been on the business for some time, you have, most likely, come across the definition of recursion, for which the factorial of a given number n! = n * n - 1 * ... * 1 is a standard example. function factorial(n) { if (n === 0) { return 1; } ... more
你可能在你建立的 React Apps 使用 Redux Store 有段時間了,當 component 經常更新時,你可能會感到很不便。 你已經徹底建立你的 state,而且架構你的每個 component 所需要的 state。然而在這些更新 state 的背後,總是在做 state 的 map 和計算。 使用 Reselector 來拯救 如何只計算我們所需要的 state?如果這個 state tree 的部分改變了,沒錯!請更新 state。 讓我看一下簡單的 TODOs List,特別是在我們的 ... more
You’ve been building your React apps with a Redux store for quite a while, yet you feel awkward when your components update so often. You’ve crafted your state thoroughly, and your architecture is such that each component gets just what it needs from ... more
This little beastie here is tap. A really useful function for quick-debugging chains of function calls, anonymous functions and, actually, whatever you just want to print. function tap(x) { console.log(x); return x; } Why would you use instead of good ... more
Arrays are everywhere in JavaScript and with the new spread operators introduced in ECMAScript 6, you can do awesome things with them. In this post I will show you 3 useful tricks you can use when programming. 1. Iterating through an empty array JavaScript ... more
In case of established websocket connection, server or firewall could timeout and terminate the connection after a period of inactivity. To deal with this situation, we send periodic message to the server. To control the timeout we will add two functions ... more
在 websocket 連接被建立的情況下,如果一段時間處於非活動狀態,伺服器或防火牆可能超時或終止連接。為了要處理這個情況,我們向伺服器週期性的傳送訊息。為了控制超時,我們將新增兩個 function 再我們的程式碼:一是確認連接持久連線(keep alive),另一個則是取消持久連線。我們也還需要一個共同的 timerID 變數。 讓我們來實作看看: var timerID = 0; function keepAlive() { var timeout = 20000; if (webSocket.readyState ... more
A common requirement of iteration is cancelation. Using for loops we can break to end iteration early. const a = [0, 1, 2, 3, 4]; for (var i = 0; i < a.length; i++) { if (a[i] === 2) { break; // stop the loop } console.log(a[i]); } //> 0, 1 Another ... more
Apart from being just a delimiter, the comma operator allows you to put multiple statements in a place where one statement is expected. Eg:- for(var i=0, j=0; i<5; i++, j++, j++){ console.log("i:"+i+", j:"+j); } Output:- i:0, j:0 ... more
This is a simple tip, this week I had to create a common “Copy to Clipboard” button, I’ve never created one before and I want to share how I made it. It’s easy, the bad thing is that we must add an <input/> with the text to be copied to the DOM. ... more
Sometimes, we need to loop endlessly over an array of items, like a carousel of images or an audio playlist. Here’s how to take an array and give it “looping powers”: var aList = ['A','B','C','D','E']; function make_looper( arr ){ arr.loop_idx = 0; // ... more
Example function where arguments 2 and 3 are optional function example( err, optionalA, optionalB, callback ) { // retrieve arguments as array var args = new Array(arguments.length); for(var i = 0; i < args.length; ++i) { args[i] = arguments[i]; }; ... more
Example function where arguments 2 and 3 are optional function example( err, optionalA, optionalB, callback ) { // retrieve arguments as array var args = new Array(arguments.length); for(var i = 0; i < args.length; ++i) { args[i] = arguments[i]; }; ... more
Question: How to get the file extension? var file1 = "50.xsl"; var file2 = "30.doc"; getFileExtension(file1); //returs xsl getFileExtension(file2); //returs doc function getFileExtension(filename) { /*TODO*/ } Solution 1: Regular ... more
Question: How to get the file extension? var file1 = "50.xsl"; var file2 = "30.doc"; getFileExtension(file1); //returs xsl getFileExtension(file2); //returs doc function getFileExtension(filename) { /*TODO*/ } Solution 1: Regular ... more
You’re going to run into some instances where you’ll be using new to allocate new objects in JavaScript. It’s going to blow your mind unless you read this tip to understand what’s happening behind the scenes. The new operator in JavaScript is an operator ... more
You’re going to run into some instances where you’ll be using new to allocate new objects in JavaScript. It’s going to blow your mind unless you read this tip to understand what’s happening behind the scenes. The new operator in JavaScript is an operator ... more
Many of us are still doing these things: element.addEventListener('type', obj.method.bind(obj)) element.addEventListener('type', function (event) {}) element.addEventListener('type', (event) => {}) The above examples all create new anonymous event ... more
Muchos de nosotros todavía están haciendo estas cosas: element.addEventListener('type', obj.method.bind(obj)) element.addEventListener('type', function (event) {}) element.addEventListener('type', (event) => {}) Los ejemplos anteriores, crean nuevos ... more