This content originally appeared on DEV Community and was authored by Bukunmi Odugbesan
The task is to implement Object.create()
The boilerplate code
function myObjectCreate(proto) {
// your code here
}
If the prototype is null or not an object, throw an error
if(proto === null || typeof !== "object") {
throw new TypeError("Prototype must be an object or null")
}
An empty constructor function is declared, because every object in Javascript is created using a constructor.
function F() {}
The prototype of the empty constructor is set to the one provided
F.prototype = proto
Then, a new instance is returned
return new F()
The final code
function myObjectCreate(proto) {
// your code here
if(proto === null || typeof proto !== "object") {
throw new TypeError("Prototype must be object or null")
}
function F() {}
F.prototype = proto;
return new F;
}
That's all folks!
This content originally appeared on DEV Community and was authored by Bukunmi Odugbesan
Bukunmi Odugbesan | Sciencx (2025-11-16T21:38:33+00:00) Coding Challenge Practice – Question 57. Retrieved from https://www.scien.cx/2025/11/16/coding-challenge-practice-question-57/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.