Create an object with the same constructor
Imagine, we have an arbitrary object obj, created by a constructor function – we don’t know which one, but we’d like to create a new object using it.
Can we do it like that?
let obj2 = new obj.constructor();
Give an example of a constructor function for obj which lets such code work right. And an example that makes it work wrong.
We can use such approach if we are sure that "constructor" property has the correct value.
For instance, if we don’t touch the default "prototype", then this code works for sure:
function User(name) {
this.name = name;
}
let user = new User('John');
let user2 = new user.constructor('Pete');
alert( user2.name ); // Pete (worked!)
It worked, because User.prototype.constructor == User.
…But if someone, so to speak, overwrites User.prototype and forgets to recreate constructor to reference User, then it would fail.
For instance:
function User(name) {
this.name = name;
}
User.prototype = {}; // (*)
let user = new User('John');
let user2 = new user.constructor('Pete');
alert( user2.name ); // undefined
Why user2.name is undefined?
Here’s how new user.constructor('Pete') works:
- First, it looks for
constructorinuser. Nothing. - Then it follows the prototype chain. The prototype of
userisUser.prototype, and it also has nothing. - The value of
User.prototypeis a plain object{}, its prototype isObject.prototype. And there isObject.prototype.constructor == Object. So it is used.
At the end, we have let user2 = new Object('Pete'). The built-in Object constructor ignores arguments, it always creates an empty object, similar to let user2 = {}, that’s what we have in user2 after all.