GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — JavaScript Function Binding - GeeksforGeeks JavaScript Function BindingLast Updated : 15 Jun, 2026 In JavaScript, function binding refers to the process of associating a function with a specific context (this value). The bind() method creates a new function that, when called, has its 'this' keyword set to the provided value.
When greet is called directly (without being bound to the person object), the value of this is not person anymore. Instead, it refers to the global object (in non-strict mode) or is undefined (in strict mode). Methods of Function Binding1. bind() MethodThe bind() method is used to create a new function that, when called, has its this value set to a specified value, regardless of how the function is invoked.
This code defines a person object with a greet method, binds it to the person object using bind(), and then calls the bound function to correctly reference the person's name when executed. 2. call() MethodThe call() method immediately invokes a function, allowing you to set the value of this and pass arguments to the function.
This code defines a greet method in the person object, calls it with 'Delhi' directly, and then uses call() to invoke the method with 'Noida', ensuring the this context is correctly bound to person. 3. apply() MethodSimilar to call(), the apply() method invokes a function and allows you to set the value of this, but the difference is that the arguments are passed as an array (or an array-like object).
This code defines a greet method in the person object, calls it with 'Delhi' and 'India', then uses apply() to invoke the method with 'Noida' and 'Delhi' by passing arguments as an array while maintaining the this context. Arrow Functions and this BindingArrow functions behave differently when it comes to the this keyword. They do not have their own this context. Instead, arrow functions inherit the this value from the surrounding lexical context.
This code defines a greet method in the person object, which contains an arrow function arrowGreet that logs the person's name using the this keyword from the enclosing greet method's context. Comment Explore |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |