Giúp dịch 1 đoạn về prototype kế thừa trong JS

The prototype link is used only in retrieval. If we try to retrieve a property value from an object, and if the object lacks the property name, then Javascript attempt to retrieve the property value from the prototype object. And if that object is lacking the property, then it goes to its prototype, and so on until the process finally bottoms out with Object.prototype. If the desired property exists nowhere in the prototype chain, then the result is the undefined value. This is called delegation

Bạn có thể đọc bài này để xem ví dụ https://developer.mozilla.org/en/docs/Web/JavaScript/Inheritance_and_the_prototype_chain

Ở mỗi object trong js đều có 1 trường là prototype. Khi bạn truy xuất 1 property trong 1 object (vd objectA) mà objectA không tồn tại trường đó, nó sẽ tìm đến trường objectA.prototype. Trường prototype này chứa các property của object cha (giả sử là objectB) mà objectA được kế thừa. Nếu objectB cũng không có, nó sẽ tiệp tục tìm đến prototype của objectB (objectA.prototype.prototype), tiếp tục như thế cho tới khi nó đến “cha của tất cả” là Object. Nếu đến Object mà vẫn không tồn tại property đó, kết quả trả về sẽ là undefined.

2 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?