Popular Posts
javax.net.ssl.SSLHandshakeException: Connection closed by peer in Android 5.0 Lollipop Recently, there is a error occurs when access website via ssl connection like below although it worked fine several days ago. // Enable SSL... Enable SSL connection for Jsoup import org.jsoup.Connection; import org.jsoup.Jsoup; import javax.net.ssl.*; import java.io.IOException; import java.security.KeyManagement... set/remove cookie using applet jdk/jre 1.4 later, the library is included in plugin.jar file. import java.applet.Applet; import java.util.ArrayList; import java.util.Date;...
Stats
Javascript lambda: No binding of this
  1. <form>
  2.     <p>
  3.         <button id="f1">Invoke with function</button>
  4.     </p>
  5.     <p>
  6.         <button id="f2">Invoke with lambda</button
  7.     </p>
  8.     <div id="output"></div>
  9. </form>
  10. <script>
  11.     'use strict';
  12.     (function () {
  13.         let f1 = document.querySelector('#f1'),
  14.             f2 = document.querySelector('#f2'),
  15.             output = document.querySelector('#output');
  16.  
  17.         function appendResult(message) {
  18.             let log = document.createElement('pre');
  19.             log.appendChild(document.createTextNode(message));
  20.             output.appendChild(log);
  21.         }
  22.  
  23.         // define with function, reference 'this' to button element
  24.         f1.onclick = function () {
  25.             appendResult('Button [Invoke with function] clicked!');
  26.             appendResult('This is: ' + JSON.stringify(this) + ' [' + this.constructor.name + ']');
  27.             console.log(this);
  28.             return false;
  29.         };
  30.  
  31.         // define with lambda, reference 'this' to {name: 'Bruce'} object
  32.         f2.onclick = () => {
  33.             appendResult('Button [Invoke with lambda] clicked!');
  34.             appendResult('This is: ' + JSON.stringify(this) + ' [' + this.constructor.name + ']');
  35.             console.log(this);
  36.             return false;
  37.         };
  38.  
  39.     }).call({name: 'Bruce'})
  40. </script>
Result:

Reference: No binding of this
Memo: Debounce Task

To prevent multi-execution from caller in short time, use debounce for single execution.

  1. var debounce = function (func, threshold, execAsap) {
  2. var timeout;
  3. return function debounced () {
  4. var obj = this, args = arguments;
  5. function delayed () {
  6. if (!execAsap)
  7. func.apply(obj, args);
  8. timeout = null;
  9. };
  10. if (timeout)
  11. clearTimeout(timeout);
  12. else if (execAsap)
  13. func.apply(obj, args);
  14. timeout = setTimeout(delayed, threshold || 100);
  15. };
  16. }