Surfing With Code

Tricks for developers about Java, HTML5, CSS, JavaScript, XML, jSON, deployment, design …

JavaScript: Passing parameters to a callback function — febrero 11, 2019

JavaScript: Passing parameters to a callback function

If you want to use a function with parameters as a callback, there are many ways to do it.

Here you are some of them:

Solution 1:

// callback function
function tryMe (param1, param2) { 
    alert (param1 + " and " + param2); 
} 

// callback executer 
function callbackTester (callback) { 
    callback(); 
} 

// test function
callbackTester (function() {
    tryMe("hello", "goodbye"); 
}); 

Solution 2:

function tryMe (param1, param2) {
    alert (param1 + " and " + param2);
}

function callbackTester (callback, param1, param2) {
    callback (param1, param2);
}

callbackTester (tryMe, "hello", "goodbye");

Solution 3:

function tryMe (param1, param2) {
    alert(param1 + " and " + param2);
}

function callbackTester (callback) {
    callback (arguments[1], arguments[2]);
}

callbackTester (tryMe, "hello", "goodbye");
How to trigger event in JavaScript? — May 23, 2018

How to trigger event in JavaScript?

In JavaScript, you can manually trigger custom events:

// Add an event listener
document.addEventListener("name-of-event", function(e) {
  console.log(e.detail); // Prints "Example of an event"
});

// Create the event
var event = new CustomEvent("name-of-event", { "detail": "Example of an event" });

// Dispatch/Trigger/Fire the event
document.dispatchEvent(event);

Source: StackOverflow

Pass a JavaScript function as parameter — noviembre 8, 2017

Pass a JavaScript function as parameter

For sure that you have ever wondered ‘How do I pass a function as a parameter without the function executing in the «parent» function?’ Easy! Just do it haha

Suppose you have a method called ‘methodExecutor’ that receives another method and execute it. The method to be execute is called ‘methodToBeExecuted’. To pass you method as parameter, you just have to add it without parenthesis.


function methodToBeExecuted(){
console.log("Hello")
}

function methodExecutor(method){
method();
}

Or if your method has params, do it as follows:


function methodToBeExecuted(txt){
console.log(txt)
}

function methodExecutor(method, txt){
method(txt);
}

Generating random number in JavaScript in a specific range — octubre 13, 2017

Generating random number in JavaScript in a specific range

There are millions of times where while we are coding we need to generate a random number. The most of the times we need it as an Integer, to get some value (position) from an array.

Here you are how to generate a random number! For more info about how does the method works, visit the SO link! Enjoy!

Source: StackOverflow

@AuthorIonuț G. Stan



/**
 * Returns a random number between min (inclusive) and max (exclusive)
 */
function getRandomArbitrary(min, max) {
 return Math.random() * (max - min) + min;
}

/**
 * Returns a random integer between min (inclusive) and max (inclusive)
 * Using Math.round() will give you a non-uniform distribution!
 */
function getRandomInt(min, max) {
 return Math.floor(Math.random() * (max - min + 1)) + min;
}