What will be output of following code:
function User(name) {
this.name = name;
}
let user1 = new User('John');
console.log(user1.name); // ?
console.log(user1.constructor === User); // ?
User.prototype = {};
let user2 = new user1.constructor('Pete');
console.log( user2.name ); // ?
console.log(user12constructor === User) // ?
Output will be:
'John'
true
'Pete'
false
user1.name
:
user1
is created with the name
'John'.'John'
user1.constructor === User
:
constructor
property points to the constructor function that created it.true
User.prototype = {};
:
User
to a new, empty object.user1.constructor('Pete')
:
user1.constructor
still points to the original User
function.user2
is created with the name
'Pete'.user2.name
: 'Pete'
user2.constructor === User
:
User
have constructor
set to Object
, because the new prototype is an empty object {}
which defaults to Object
.false