Trung BìnhJavaScript iconJavaScript

Object.create() hoạt động như thế nào?

Object.create(proto) tạo object mới với prototype là proto.

Object con dùng được method từ proto qua prototype chain.

javascript
const parent = { greet() { return 'Hi!'; } };
const child = Object.create(parent);
child.greet(); // 'Hi!' — inherited
child.hasOwnProperty('greet'); // false — not own

// Object.create(null) — object không có prototype
const dict = Object.create(null);
'toString' in dict; // false — thật sự sạch

Xem toàn bộ JavaScript cùng filter theo level & chủ đề con.

Mở danh sách JavaScript