Currently reading js.info and came upon this:
"Let’s note once again – technically, any function (except arrow functions, as they don’t have this) can be used as a constructor. It can be run with new, and it will execute the algorithm above. The “capital letter first” is a common agreement, to make it clear that a function is to be run with new."
This is the function they included but it doesn't seem to portray what they described. I also couldn't get it to run at all (several errors).
// create a function and immediately call it with new
let user = new function() {
this.name = "John";
this.isAdmin = false;
// ...other code for user creation
// maybe complex logic and statements
// local variables etc
};
I'm curious what this would look like in practice and what use cases exist for a constructor function that does not return/pertain to an object?
CodePudding user response:
As for use cases: constructor functions are de facto object factories. It might be that you want to hide complicated/non-standard construction details from client code while maintaining a familiar construction API using new
. The ability to return any object you like from a constructor function invoked with new
enables this.
More info
Let's rearrange your code to make it a little easier to follow (this is only marginally different in that the constructor function now will have an inferred name 'F'
):
let F = function() {
this.name = "John";
this.isAdmin = false;
// ...other code for user creation
// maybe complex logic and statements
// local variables etc
};
let user = new F; // or `new F();` if you prefer, parens are optional
console.log(Object.getPrototypeOf(user) === F.prototype) // true
When F
is invoked with new
:
- A new object
o
is immediately created, with its [[Prototype]] set to refer to the.prototype
property of functionF
. - The
this
value inside F is set to beo
for the duration of the invocation ofF
as a constructor function. - The
.constructor
property ofo
is set to point toF
- The return value of
F
is automatically set to beo
if no value is explicitly returned.
Most constructor functions do not explicitly return an object. This is because invocation with new
implicitly creates a new object, o
, configured according to the rules of prototypical inheritance; o
is set to be the this
value used for the duration of the execution of the constructor function, and o
is automatically and implicitly returned if no value is explicitly returned.
That said, programmers can instead explicitly return any object they like from a constructor function, and this overrides and prevents the implicit return.
Any function that implements the [[Construct]]
internal method can be used as a constructor function with new
(and super
).
Ordinary function declarations (function foo() { ... }
) and class constructors (class foo { constructor() {} }
) can be used as constructor functions. Fat arrow functions, object literal methods, async functions and class methods cannot be used as constructor functions because they don't have the [[Construct]]
internal method.