Closed jsartisan closed 2 months ago
difficulty: easy title: Class and Prototype type: quiz tags: css, html, javascript
What will be output of following code:
class User { constructor(name) { this.name = name; } greet() { console.log(`Hello ${this.name}`); } } console.log(typeof User); console.log(User.prototype); console.log(User.prototype.constructor); console.log(Object.getOwnPropertyNames(User.prototype));
This challenge tests your knowledge about how "class" syntax actually do under the hood.
function { constructor: [Function: User], greet: [Function: greet] } [Function: User] ['constructor', 'greet']
typeof User:
typeof User
User.prototype:
User.prototype
constructor
greet
User
User.prototype.constructor:
User.prototype.constructor
Object.getOwnPropertyNames(User.prototype):
Object.getOwnPropertyNames(User.prototype)
Info
Question
What will be output of following code:
This challenge tests your knowledge about how "class" syntax actually do under the hood.
Solution
Explanation
typeof User
:typeof User
will return `"function".User.prototype
:constructor
andgreet
. Theconstructor
property references theUser
function itself.User.prototype.constructor
:User
function, indicating that theconstructor
property points to theUser
function.Object.getOwnPropertyNames(User.prototype)
:User.prototype
, which areconstructor
andgreet
.