Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Constructor Function

Object-Oriented Programmingmedium

Course · Section 14: Object-Oriented Programming (OOP) With JavaScript · Lecture 221: Constructor Functions and the new Operator

Using a constructor function `User(name, age)` with the `new` operator, implement `makeUser(name, age)` returning a new user object with `name` and `age` properties.

Sample tests

Input: makeUser("Ada", 30)
Output: {"name":"Ada","age":30}
Input: makeUser("Bo", 5)
Output: {"name":"Bo","age":5}

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Forgetting new leaves this undefined and returns nothing useful.
Approach & explanation (try first)

new creates a fresh object, binds it to this in the constructor, and returns it with the assigned properties.

Loading...
⌘/Ctrl + Enter

Run your code to see results.