Why is passing function in parameters of another function is giving unexpected result in Javascript

pass value from one function to another function in javascript
javascript pass function as parameter with arguments
pass parameter to javascript function onclick
javascript function parameters
javascript pass variable number of arguments to function
how to use one function variable in another function in javascript
javascript optional parameters
javascript pass function with parameters as callback

Please find the code below whenever i am trying to run this one it's giving some random integer value why is that . Can someone explain what is happening in the output.

let user = {
 name: "John",
 sayHi: function(){
  console.log(this.name);
 }
}
let paramFunc = function(func){
 func();
}
paramFunc(user.sayHi);

When you just call a function without context, the function doesn't know about the object.

you need to provide the context, so the function knows what this means:

let user = {
    name: "John",
    sayHi: function () {
        console.log(this.name);
    }
}
let paramFunc = function (func, context) {
    func.bind(context)();
};
paramFunc(user.sayHi, user);

Output:

John

Chapter 15. Functions, You can call a function by mentioning its name, followed by arguments in parentheses: in the last example), passed as an argument to another function, and more. and Detection of Properties) to check whether arguments “has” a given index: then you can get unexpected results: c could have optional parameters that� Assign function Recursive function Passing functions as parameter of another function. examples/functions/passing_function.js

Bind the user object when passing the function that will help as the reference of this changes when you are running in another function

let user = {
 name: "John",
 sayHi: function(){
  console.log(this.name);
 }
}
let paramFunc = function(func){
 func();
}
paramFunc(user.sayHi.bind(user));

Default parameters, JavaScript Demo: Functions Default In JavaScript, function parameters default to undefined . Passing undefined vs. other falsy values. This is the most appropriate use of lambda expressions: passing a function into another function while defining that passed function all on one line of code.. As I’ve written about in Overusing lambda expressions, I’m not a fan of Python’s lambda expression syntax.

Well , I was not trying to print "John" inside the console i was just trying to print what does the this.name gives to me when called in this way . So i just checked why this.name is not giving error rather it's printing an empty string . Because this is pointing to the window object and by default window object has a name property which is set to empty initially . That's the reason calling func() will access the global this or window which is giving the name property as empty string .

Functions, Primitive parameters (such as a number) are passed to functions by value; the Providing a name allows the function to refer to itself, and also makes it easier to function map(f, a) { let result = []; // Create a new Array let i; // Declare variable A function defined inside another function can also access all� Javascript allows you to pass functions as parameter to another function and that makes Javascript super awesome!. Great things can be achieved from this power. Quote from Mozilla developer website: In JavaScript, functions are first-class objects, i.e. they are objects and can be manipulated and passed around just like any other object.

JavaScript function that accepts any number of arguments, How can you create a function in JavaScript that will accept an As you can observe, the first two calls gave the correct result. The third one, where we passed 3 numbers return 2, which might be We can try to create another add function that will accept 3 parameters and will return the sum of all 3. If behaviour of omittting the parameter in not docummented, or there is no default parameter, you are in great trouble. You can't sleep well if you update the external library. Only god and maybe library developer can know what will happen if you pass anything unexpected instead of the required parameter.

JavaScript Function Parameters, Function arguments are the real values passed to (and received by) the function. Parameter Rules. JavaScript function definitions do not specify data types for� Use Function.prototype.bind().Quoting MDN: The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.

Organizing Your JavaScript Code with Functions, The return statement lets the result of the function be sent to another variable or as an There are 2 ways to pass in arguments in JavaScript. function parameters for optional parameters to avoid unexpected results. In this example, we wrote a function to compute the sum of squares for a given number. Passing a function as an argument to the function is quite similar to the passing variable as an argument to the function. Below examples describes to passing a function as a parameter to another function. Example 1: This example passes a function geeks_inner to the function geeks_outer as an argument.

Comments
  • Try to debug this code. And look what the value of this when you run paramFunc.