Outils pour utilisateurs

Outils du site


webdev:javascript:basics

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
webdev:javascript:basics [31/10/2018 20:09]
dolo
webdev:javascript:basics [02/12/2020 21:20] (Version actuelle)
dolo [Basics]
Ligne 1: Ligne 1:
 ====== Basics ====== ====== Basics ======
 +  * [[https://stackoverflow.com/questions/24816/escaping-html-strings-with-jquery#answer-12034334|Escape HTML]]
 +  * [[https://www.sitepoint.com/delay-sleep-pause-wait/|Delay sleep pause wait]] - or lack thereof [[https://www.w3schools.com/js/js_timing.asp|Doc]]
 +  * ...
  
-Get the length of an object (ES5) :  +==== Events ==== 
-<Code linenums> +  * [[https://www.w3schools.com/tags/ref_eventattributes.asp|Basic HTML events]] 
-var size Object.keys(myObj).length; +  * [[https://developer.mozilla.org/en-US/docs/Web/Events|Basic javascript events]] 
-</Code> +  * [[https://medium.com/@jacobwarduk/how-to-correctly-use-preventdefault-stoppropagation-or-return-false-on-events-6c4e3f31aedb|How to correctly use preventDefault(), stopPropagation(), or return false; on events]]
- +
-==== HTML events ==== +
-  * [[https://www.w3schools.com/tags/ref_eventattributes.asp|Basics]]+
  
   * On Chrome **e.originalEvent.screenX** take different values than **e.originalEvent.clientX**, unlike Firefox.   * On Chrome **e.originalEvent.screenX** take different values than **e.originalEvent.clientX**, unlike Firefox.
  
 +== Pass additionnal parameters to an event using jQuery .trigger() ==
 +<Code linenums>
 +// Trigger
 +$('.volume').on('mousemove', function(e)
 +{
 + console.log(e);
 + console.log(e.pageX);
 + $('.volume').trigger('mousedown', [e.pageX] );
 +});
 +
 +// We get the value as
 +$('.volume').on('mousedown', function(e, altPageX)
 +{
 + console.log(e);
 + console.log(altPageX);
 +});
 +</Code>
 +== Debouncing / Throttle ==
 +  * [[https://eloquentjavascript.net/15_event.html#h_AOVmaqj10I|Source]]
 +Throttle : Similar to debouncing but ensure a minimum execution of the function every fixed time
 +  * [[https://css-tricks.com/debouncing-throttling-explained-examples/#article-header-id-5|Source]]
 +
 +Using jQuery's .one() can also work.
 ==== Promises ==== ==== Promises ====
   * https://javascript.info/promise-basics   * https://javascript.info/promise-basics
   * https://javascript.info/promise-chaining   * https://javascript.info/promise-chaining
 +
 +==== Check if a variable exist in javascript without getting an error ====
 +[[https://stackoverflow.com/questions/858181/how-to-check-a-not-defined-variable-in-javascript#answer-858193|Source]]
 +
 +<Code linenums>
 +if( typeof google_step != 'undefined')
 +{
 + // ...
 +}
 +</Code>
 +
 +==== Objects ====
 +=== Object copy and references ===
 +  * [[https://stackoverflow.com/questions/16880418/javascript-pass-object-as-reference|Basic copy with = is just a reference, not new object]]
 +<Code linenums>
 + var tmpDisable = {};
 + var lastTmpDisable = {};
 +
 + lastTmpDisable = tmpDisable; // Any change made to any of these object will be on both because they're references
 +
 + // If we declare it this way a new object is created
 + for(var key in tmpDisable)
 + {
 + lastTmpDisable[key] = tmpDisable[key]
 + }
 +</Code>
 +
 +=== Get the length of an object (ES5) ===
 +<Code linenums>
 +var size = Object.keys(myObj).length;
 +</Code>
webdev/javascript/basics.1541012986.txt.gz · Dernière modification: 31/10/2018 20:09 de dolo