Monday, November 11, 2024

JavaScript Regular function call vs Cnstructor function call

Question1

In JavaScript, what happens when the following function A is called in either way:
function A (){}
A();
new A();
In JavaScript, calling the function A in these two ways has different implications:

A() - This is a normal function call. Here, A is invoked without the new keyword, so it behaves as a regular function. Inside the function, this will refer to the global object (window in browsers, global in Node.js), unless the function is in strict mode ('use strict'), in which case this will be undefined.

new A() - This is a constructor call. When A is called with the new keyword, JavaScript creates a new object and sets this inside A to refer to that new object. This allows the function A to act as a constructor, initializing properties on the new object. new A() returns the new object, even if A has no explicit return statement.

In summary:
  • A() is a plain function call.
  • new A() creates a new instance of A (an object with A as its constructor).


Question2

function A (){} and function B(){} then Is new A() == new B()?

Answer: No, new A() == new B() will return false in JavaScript. Here’s why:
When you use new with a function like A or B, it creates a new, distinct object each time. So, new A() creates a new object, and new B() creates another separate object. Even if the functions A and B have identical code, each new instance creates a unique object in memory. 

In JavaScript, == and === comparisons between objects only return true if they refer to the exact same object in memory, not just two objects that happen to have the same properties. For example:
function A() {}
function B() {}

console.log(new A() == new B()); // false
console.log(new A() === new B()); // false
Each new A() and new B() instance points to a unique object, so they are not considered equal.

Question3

let obj = {}; function A (){}  then can we write like this: new A() = obj;?

No, you cannot write new A() = obj; in JavaScript. This syntax is invalid because new A() creates a new object instance, which cannot be assigned to another object directly using =. The left side of an assignment (=) must be a variable, a property, or an array element, not the result of an expression like new A().

If you want obj to refer to the instance created by new A(), you can do it like this:
let obj = new A();
Now, obj will hold the reference to the instance created by new A().

Question4

What is difference among the three following:
  1. function A (){} 
  2. function A (){return;} 
  3. function A (){return this;} 
Answer: These three versions of the function A have subtle but important differences in behavior due to how they handle the return statement. Here’s a breakdown:

1. function A() {}

This is a simple function with no return statement.
When called as A(), it implicitly returns undefined, as all JavaScript functions do when there’s no explicit return statement.
When called as new A(), it returns a new instance of A, meaning a new object with A as its constructor.
function A() {}
console.log(A());       // undefined (normal call)
console.log(new A());   // A {} (new object)
2. function A() { return; }

This function has an explicit return; statement with no value, so it behaves almost identically to the first example.
  • When called as A(), it returns undefined explicitly.
  • When called as new A(), it also returns a new instance of A, just as if the return statement wasn’t there. 
This is because in JavaScript, a constructor (new A()) ignores any return statement that returns undefined or a primitive value.
function A() { return; }
console.log(A());       // undefined (normal call)
console.log(new A());   // A {} (new object)
3. function A() { return this; }

Here, A returns this, which depends on the context of the call.
When called as A(), this typically refers to the global object (window in browsers, global in Node.js) or is undefined in strict mode. So, A() returns the global object (or undefined in strict mode).
When called as new A(), this refers to the newly created instance, so it returns that instance itself (the same as what new would have returned by default).
function A() { return this; }
console.log(A());       // global object (or undefined in strict mode)
console.log(new A());   // A {} (the instance itself)

Summary

  • function A() {} and function A() { return; } behave the same with new, as they both implicitly or explicitly return undefined.
  • function A() { return this; } explicitly returns the current this, making A() and new A() both return the calling context (this).

No comments:

Post a Comment

Hot Topics