المسارات / JavaScript / المرجع / Object.setPrototypeOf

مرجع · Object

Object.setPrototypeOf

Object.setPrototypeOf بتغيّر object بعد إنشائه، وبترجّع نفس الـobject.

What does Object.setPrototypeOf() do in JavaScript?

js
Object.setPrototypeOf(obj, proto)

بتشتغل، بس بطيئة على الـobjects اللي بتتكرر لأنها بتكسر الشكل الداخلي للـobject. لو محتاج وراثة، اعمل الـobject بـObject.create من الأول.

البرهان

شُغّل على Node v26.14.0، والناتج منسوخ من التشغيل نفسه.

js
const base = { hello: () => 'أهلا' };
const o = {};
Object.setPrototypeOf(o, base);
console.log(o.hello(), Object.getPrototypeOf(o) === base);
النتيجة
أهلا true

الدروس اللي بتشرحها

الصفحة دي على MDN ↗

ابعت الصفحة لحد
واتسابتليجرامX
برهان

الدروس

اتحلّ 0 من 174 تمرين