Table des matières

Basics

Events

Pass additionnal parameters to an event using jQuery .trigger()
  1. // Trigger
  2. $('.volume').on('mousemove', function(e)
  3. {
  4. console.log(e);
  5. console.log(e.pageX);
  6. $('.volume').trigger('mousedown', [e.pageX] );
  7. });
  8.  
  9. // We get the value as
  10. $('.volume').on('mousedown', function(e, altPageX)
  11. {
  12. console.log(e);
  13. console.log(altPageX);
  14. });
Debouncing / Throttle

Throttle : Similar to debouncing but ensure a minimum execution of the function every fixed time

Using jQuery's .one() can also work.

Promises

Check if a variable exist in javascript without getting an error

Source

  1. if( typeof google_step != 'undefined')
  2. {
  3. // ...
  4. }

Objects

Object copy and references

  1. var tmpDisable = {};
  2. var lastTmpDisable = {};
  3.  
  4. lastTmpDisable = tmpDisable; // Any change made to any of these object will be on both because they're references
  5.  
  6. // If we declare it this way a new object is created
  7. for(var key in tmpDisable)
  8. {
  9. lastTmpDisable[key] = tmpDisable[key]
  10. }

Get the length of an object (ES5)

  1. var size = Object.keys(myObj).length;