Cơ BảnJavaScript iconJavaScript

__proto__ và prototype khác nhau như thế nào?

__proto__ là instance link — nằm trên object instance. prototype là blueprint — nằm trên constructor function.

Khi dùng new Foo(), object mới có __proto__ trỏ tới Foo.prototype.

javascript
function Dog(name) { this.name = name; }
Dog.prototype.bark = function() { return 'Woof'; };

const d = new Dog('Rex');
d.__proto__ === Dog.prototype; // true
Object.getPrototypeOf(d) === Dog.prototype; // true (cách khuyến nghị)

Trong code thực tế, dùng Object.getPrototypeOf(obj) thay vì truy cập __proto__ trực tiếp.

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

Mở danh sách JavaScript