Invocation & 'this'
Master this topic with zero to advance depth.
Expert Answer & Key Takeaways
Mastering Invocation & 'this' is essential for high-fidelity technical architecture and senior engineering roles in 2026.
JavaScript Function Invocation
The code inside a JavaScript function is not executed when the function is defined. It is executed when the function is invoked.
1. Invoking a Function as a Function
This is the global way to call a function.
function myFunction(a, b) {
return a * b;
}
myFunction(10, 2); // Will return 20In a browser, the global object is the browser window. The function above automatically becomes a window function.
myFunction() and window.myFunction() are the same.2. The this Keyword
In JavaScript, the thing called
this, is the object that "owns" the current code.
The value of this, when used in a function, is the object that "owns" the function.3. Invoking a Function as a Method
In JavaScript, you can define functions as object methods.
const myObject = {
firstName: "John",
lastName: "Doe",
fullName: function () {
return this.firstName + " " + this.lastName;
}
}
myObject.fullName(); // Will return "John Doe"When the function is called as a method, the
this keyword refers to the object itself (myObject).4. Invoking a Function with a Function Constructor
If a function invocation is preceded by the
new keyword, it is a constructor invocation.// This is a function constructor:
function myFunction(arg1, arg2) {
this.firstName = arg1;
this.lastName = arg2;
}
// This creates a new object:
const myObj = new myFunction("John", "Doe");
// This will return "John":
myObj.firstName;A constructor invocation creates a new object. The new object inherits the properties and methods from its constructor.
Top Interview Questions
?Interview Question
Q:What is the global object in a browser environment?
A:
In a web browser, the global object is 'window'. All global variables and functions automatically become members of the window object.
?Interview Question
Q:What does 'this' refer to in a method invocation?
A:
When a function is called as a method of an object (e.g., myObj.myFunc()), 'this' refers to the object that 'owns' the method (myObj).
?Interview Question
Q:What happens when you use 'new' with a function?
A:
The 'new' keyword creates a new, empty object, sets 'this' to point to that object within the function, and then returns that new object after the function runs.
Course4All Engineering Team
Verified ExpertSenior Full-Stack Engineers & V8 Experts
Our JavaScript and engine-level content is developed by a collective of senior engineers focused on high-performance web architecture and 2026 standards.
Pattern: 2026 Ready
Updated: Weekly
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.