How can you clone an object and preserve its prototype chain?
function cloneObject(obj) {
const clone = Object.create(Object.getPrototypeOf(obj));
Object.getOwnPropertyNames(obj).forEach(prop => {
const desc = Object.getOwnPropertyDescriptor(obj, prop);
Object.defineProperty(clone, prop, desc);
});
return clone;
}
const obj1 = { name: "John", age: 30 };
const obj2 = cloneObject(obj1);
console.log(obj2.name);