Outils pour utilisateurs

Outils du site


Panneau latéral

webdev:javascript:basics

Basics

Events

  • On Chrome e.originalEvent.screenX take different values than e.originalEvent.clientX, unlike Firefox.
Pass additionnal parameters to an event using jQuery .trigger()
// 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);
});
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

if( typeof google_step != 'undefined')
{
	// ...
}

Objects

Object copy and references

	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]
	}

Get the length of an object (ES5)

var size = Object.keys(myObj).length;
webdev/javascript/basics.txt · Dernière modification: 02/12/2020 21:20 de dolo