Coding Challenge Practice – Question 45

The task is to implement myNew function to return an object just like the new constructor.

const myNew = (constructor, …args) => {
// your code here
}

How does the new operator work? It creates a new empty object, sets the prototype of …


This content originally appeared on DEV Community and was authored by Bukunmi Odugbesan

The task is to implement myNew function to return an object just like the new constructor.

const myNew = (constructor, ...args) => {
  // your code here
}

How does the new operator work? It creates a new empty object, sets the prototype of that object to the constructor's prototype, calls the constructor function, and returns the object the constructor returns or the newly created one.

If the constructor is not a function, throw an error

if (typeof constructor !== 'function') {
    throw new TypeError('myNew: first argument must be a constructor function');
  }

Create the new object to be returned if the constructor does not return one

const obj = Object.create(constructor.prototype);

Call the constructor with "this" set to the new object

const result = constructor.apply(obj, args);

If the constructor returns an object (or function), return it. Otherwise, return the new function

return (result !== null && (typeof result === 'object' || typeof result === 'function')) 
    ? result 
    : obj;

The final code

const myNew = (constructor, ...args) => {
  // your code here
  if(typeof constructor !== 'function') {
    throw new TypeError('myNew: first argumant must be a function')
  }
  const obj = Object.create(constructor.prototype);

  const result = constructor.apply(obj, args)

  return (result !== null && (typeof result === 'object' || typeof result === 'function'))
  ? result : obj;
}

That's all folks!


This content originally appeared on DEV Community and was authored by Bukunmi Odugbesan


Print Share Comment Cite Upload Translate Updates
APA

Bukunmi Odugbesan | Sciencx (2025-11-04T21:28:57+00:00) Coding Challenge Practice – Question 45. Retrieved from https://www.scien.cx/2025/11/04/coding-challenge-practice-question-45/

MLA
" » Coding Challenge Practice – Question 45." Bukunmi Odugbesan | Sciencx - Tuesday November 4, 2025, https://www.scien.cx/2025/11/04/coding-challenge-practice-question-45/
HARVARD
Bukunmi Odugbesan | Sciencx Tuesday November 4, 2025 » Coding Challenge Practice – Question 45., viewed ,<https://www.scien.cx/2025/11/04/coding-challenge-practice-question-45/>
VANCOUVER
Bukunmi Odugbesan | Sciencx - » Coding Challenge Practice – Question 45. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/11/04/coding-challenge-practice-question-45/
CHICAGO
" » Coding Challenge Practice – Question 45." Bukunmi Odugbesan | Sciencx - Accessed . https://www.scien.cx/2025/11/04/coding-challenge-practice-question-45/
IEEE
" » Coding Challenge Practice – Question 45." Bukunmi Odugbesan | Sciencx [Online]. Available: https://www.scien.cx/2025/11/04/coding-challenge-practice-question-45/. [Accessed: ]
rf:citation
» Coding Challenge Practice – Question 45 | Bukunmi Odugbesan | Sciencx | https://www.scien.cx/2025/11/04/coding-challenge-practice-question-45/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.