-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
Function invocation means executing or calling a function. The way you invoke a function in JavaScript determines what value this
refers to and how the function behaves.
When a function is called directly:
function show() {
console.log("Hello");
}
show(); // Output: Hello
In non-strict mode,
this
refers to the global object (window
in browsers).
In strict mode,this
isundefined
.
When a function is called as a method of an object:
const person = {
name: "Alice",
greet: function () {
console.log("Hello " + this.name);
},
};
person.greet(); // Output: Hello Alice
Here,
this
refers to the object before the dot (person
).
new
)Functions can be used as constructors to create objects:
function Person(name) {
this.name = name;
}
const p1 = new Person("John");
console.log(p1.name); // Output: John
When a function is invoked with
new
, a new object is created andthis
refers to that new object.
call
, apply
, bind
)These methods let you manually control the value of this
.
function sayHello() {
console.log("Hello " + this.name);
}
const user = { name: "Bob" };
sayHello.call(user); // Output: Hello Bob
sayHello.apply(user); // Output: Hello Bob
const boundFunc = sayHello.bind(user);
boundFunc(); // Output: Hello Bob
Arrow functions don’t have their own this
; they inherit from the parent scope:
const user = {
name: "Eve",
greet: () => {
console.log("Hi " + this.name);
},
};
user.greet(); // Output: Hi undefined
Arrow functions are not suitable for methods if you rely on
this
.
Invocation Type | Description | this Value |
---|---|---|
Regular Function | Called normally | Global (non-strict) / undefined (strict) |
Method Invocation | Called via object | The object itself |
Constructor (new ) |
Creates new instance | New object created |
call() / apply() |
Calls with custom this |
User-defined |
bind() |
Returns new function with bound this |
Bound object |
Arrow Function | Inherits from surrounding context | Lexical this |
Q1. What is function invocation in JavaScript, and how does it work?
Q2. What happens to this
when a function is invoked normally in non-strict mode?
Q3. Write an example where a function is invoked as a method of an object.
Q4. How does using new
change how a function is invoked?
Q5. Explain the difference between call()
and apply()
with examples.
Q6. What is the role of bind()
in function invocation?
Q7. Why does an arrow function return undefined
for this
in a method?
Q8. Write a constructor function that creates a Car
object with a brand
property.
Q9. What will be the output if a function is invoked with more arguments than parameters?
Q10. Explain how function invocation affects the behavior of this
.
Q1: Which of the following is a correct function invocation?
Q2: What does this refer to in a regular function call (non-strict mode)?
Q3: How is this determined in a method call?
Q4: Which function invocation creates a new object?
Q5: Which method lets you set this manually and call the function immediately?
Q6: What does bind() return?
Q7: What will this refer to inside an arrow function?
Q8: Which invocation allows an object to temporarily borrow a method?
Q9: Which statement is true about function constructors?