{"id":39549,"date":"2023-09-15T12:08:14","date_gmt":"2023-09-15T05:08:14","guid":{"rendered":"https:\/\/itviec.com\/blog\/?p=39549"},"modified":"2023-09-15T12:08:14","modified_gmt":"2023-09-15T05:08:14","slug":"prototype-chain-la-gi","status":"publish","type":"post","link":"https:\/\/itviec.com\/blog\/prototype-chain-la-gi\/","title":{"rendered":"Prototype chain l\u00e0 g\u00ec?"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">N\u1ed9i dung b\u00e0i vi\u1ebft<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/itviec.com\/blog\/prototype-chain-la-gi\/#Prototype_chain_la_gi\" >Prototype chain l\u00e0 g\u00ec?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/itviec.com\/blog\/prototype-chain-la-gi\/#Tinh_ke_thua_va_prototype_chain\" >T\u00ednh k\u1ebf th\u1eeba v\u00e0 prototype chain<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/itviec.com\/blog\/prototype-chain-la-gi\/#Tong_ket\" >T\u1ed5ng k\u1ebft<\/a><\/li><\/ul><\/nav><\/div>\n<p><em><strong>Prototype chain l\u00e0 m\u1ed9t kh\u00e1i ni\u1ec7m quan tr\u1ecdng khi b\u1ea1n t\u00ecm hi\u1ec3u v\u1ec1 <a href=\"https:\/\/itviec.com\/blog\/prototype-trong-javascript-la-gi\/\" target=\"_blank\" rel=\"noopener\">prototype<\/a> trong JavaScript. \u0110\u1ecdc b\u00e0i vi\u1ebft n\u00e0y \u0111\u1ec3 hi\u1ec3u prototype chain l\u00e0 g\u00ec, t\u00ednh k\u1ebf th\u1eeba trong prototype chain th\u1ec3 hi\u1ec7n nh\u01b0 th\u1ebf n\u00e0o. T\u1eeb \u0111\u00f3, n\u1eafm v\u1eefng l\u00fd thuy\u1ebft v\u00e0 c\u00e1ch \u00e1p d\u1ee5ng prototype chain.<\/strong><\/em><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Prototype_chain_la_gi\"><\/span><strong>Prototype chain l\u00e0 g\u00ec?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Trong <a href=\"https:\/\/itviec.com\/blog\/javascript-la-gi\/\" target=\"_blank\" rel=\"noopener\">JavaScript<\/a>, prototype chain l\u00e0 m\u1ed9t c\u01a1 ch\u1ebf cho ph\u00e9p c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng truy c\u1eadp v\u00e0o c\u00e1c thu\u1ed9c t\u00ednh v\u00e0 ph\u01b0\u01a1ng th\u1ee9c \u0111\u01b0\u1ee3c \u0111\u1ecbnh ngh\u0129a trong c\u00e1c \u0111\u1ed1i t\u01b0\u1ee3ng prototype c\u1ee7a ch\u00fang. M\u1ed7i \u0111\u1ed1i t\u01b0\u1ee3ng trong JavaScript c\u00f3 m\u1ed9t li\u00ean k\u1ebft \u1ea9n v\u1edbi prototype c\u1ee7a n\u00f3.<\/p>\n<p>N\u1ebfu m\u1ed9t object c\u1ed1 g\u1eafng truy c\u1eadp c\u00f9ng m\u1ed9t thu\u1ed9c t\u00ednh c\u00f3 trong h\u00e0m constructor v\u00e0 object prototype, th\u00ec object \u0111\u00f3 s\u1ebd l\u1ea5y thu\u1ed9c t\u00ednh t\u1eeb h\u00e0m constructor.<\/p>\n<p>V\u00ed d\u1ee5:<\/p>\n<pre>function Person() {\r\n\r\n\u00a0\u00a0\u00a0\u00a0this.name = 'John'\r\n\r\n}\r\n\r\n\r\n\/\/ th\u00eam thu\u1ed9c t\u00ednh\u00a0\r\n\r\nPerson.prototype.name = 'Peter';\r\n\r\nPerson.prototype.age = 23\r\n\r\n\r\nconst person1 = new Person();\r\n\r\n\r\nconsole.log(person1.name); \/\/ John\r\n\r\nconsole.log(person1.age); \/\/ 23<\/pre>\n<p>Trong ch\u01b0\u01a1ng tr\u00ecnh tr\u00ean, m\u1ed9t t\u00ean thu\u1ed9c t\u00ednh \u0111\u01b0\u1ee3c khai b\u00e1o trong h\u00e0m constructor v\u00e0 c\u1ea3 trong thu\u1ed9c t\u00ednh prototype c\u1ee7a h\u00e0m constructor.<\/p>\n<p>Khi ch\u01b0\u01a1ng tr\u00ecnh th\u1ef1c thi, person1.name t\u00ecm trong h\u00e0m kh\u1edfi t\u1ea1o xem c\u00f3 thu\u1ed9c t\u00ednh t\u00ean name hay kh\u00f4ng. V\u00ec h\u00e0m constructor c\u00f3 thu\u1ed9c t\u00ednh t\u00ean v\u1edbi gi\u00e1 tr\u1ecb &#8216;John&#8217;, n\u00ean object s\u1ebd l\u1ea5y gi\u00e1 tr\u1ecb t\u1eeb thu\u1ed9c t\u00ednh \u0111\u00f3.<\/p>\n<p>Khi ch\u01b0\u01a1ng tr\u00ecnh th\u1ef1c thi, person1.age t\u00ecm trong h\u00e0m kh\u1edfi t\u1ea1o \u0111\u1ec3 xem c\u00f3 thu\u1ed9c t\u00ednh n\u00e0o t\u00ean l\u00e0 age hay kh\u00f4ng. V\u00ec h\u00e0m constructor kh\u00f4ng c\u00f3 thu\u1ed9c t\u00ednh age n\u00ean ch\u01b0\u01a1ng tr\u00ecnh xem x\u00e9t object prototype c\u1ee7a h\u00e0m constructor v\u00e0 object k\u1ebf th\u1eeba thu\u1ed9c t\u00ednh t\u1eeb object prototype (n\u1ebfu c\u00f3).<\/p>\n<p>L\u01b0u \u00fd: B\u1ea1n c\u0169ng c\u00f3 th\u1ec3 truy c\u1eadp thu\u1ed9c t\u00ednh prototype c\u1ee7a h\u00e0m constructor t\u1eeb m\u1ed9t object.<\/p>\n<pre>function Person () {\r\n\r\n\u00a0\u00a0\u00a0\u00a0this.name = 'John'\r\n\r\n}\r\n\r\n\r\n\/\/ th\u00eam 1 prototype\r\n\r\nPerson.prototype.age = 24;\r\n\r\n\r\n\/\/ t\u1ea1o object\r\n\r\nconst person = new Person();\r\n\r\n\r\n\/\/ truy c\u1eadp thu\u1ed9c t\u00ednh prototype\r\n\r\nconsole.log(person.__proto__); \u00a0 \/\/ { age: 24 }<\/pre>\n<p>Trong v\u00ed d\u1ee5 tr\u00ean, m\u1ed9t object ng\u01b0\u1eddi \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 truy c\u1eadp thu\u1ed9c t\u00ednh prototype b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng <code>__proto__<\/code>. Tuy nhi\u00ean, <code>__proto__<\/code> kh\u00f4ng \u0111\u01b0\u1ee3c d\u00f9ng n\u1eefa n\u00ean b\u1ea1n h\u00e3y tr\u00e1nh s\u1eed d\u1ee5ng ch\u00fang khi code.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Tinh_ke_thua_va_prototype_chain\"><\/span><strong>T\u00ednh k\u1ebf th\u1eeba v\u00e0 prototype chain<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><strong>Thu\u1ed9c t\u00ednh k\u1ebf th\u1eeba<\/strong><\/h3>\n<p>C\u00e1c object trong JavaScript l\u00e0 c\u00e1c &#8220;t\u00fai&#8221; thu\u1ed9c t\u00ednh \u0111\u1ed9ng (dynamic properties, ho\u1eb7c thu\u1ed9c t\u00ednh ri\u00eang &#8211; own properties). Ch\u00fang li\u00ean k\u1ebft \u0111\u1ebfn m\u1ed9t prototype.<\/p>\n<p>Khi b\u1ea1n mu\u1ed1n truy c\u1eadp m\u1ed9t thu\u1ed9c t\u00ednh c\u1ee7a m\u1ed9t object, tr\u01b0\u1edbc ti\u00ean, qu\u00e1 tr\u00ecnh t\u00ecm ki\u1ebfm thu\u1ed9c t\u00ednh s\u1ebd b\u1eaft \u0111\u1ea7u t\u1eeb trong ch\u00ednh object \u0111\u00f3. N\u1ebfu kh\u00f4ng t\u00ecm th\u1ea5y thu\u1ed9c t\u00ednh \u0111\u00f3, s\u1ebd ti\u1ebfp t\u1ee5c t\u00ecm ki\u1ebfm trong prototype c\u1ee7a object. N\u1ebfu v\u1eabn kh\u00f4ng t\u00ecm th\u1ea5y, l\u1ea1i ti\u1ebfp t\u1ee5c t\u00ecm trong prototype c\u1ee7a prototype \u0111\u00f3. V\u00e0 qu\u00e1 tr\u00ecnh n\u00e0y ti\u1ebfp t\u1ee5c cho \u0111\u1ebfn khi thu\u1ed9c t\u00ednh \u0111\u00f3 \u0111\u01b0\u1ee3c t\u00ecm th\u1ea5y ho\u1eb7c cho \u0111\u1ebfn khi h\u1ebft chu\u1ed7i prototype \u0111\u00f3 (g\u1ecdi l\u00e0 prototype chain), l\u00fac n\u00e0y s\u1ebd tr\u1ea3 v\u1ec1 undefined.<\/p>\n<p>C\u00f3 m\u1ed9t s\u1ed1 c\u00e1ch \u0111\u1ec3 ch\u1ec9 \u0111\u1ecbnh <code>[[Prototype]]<\/code> c\u1ee7a m\u1ed9t object, s\u1ebd \u0111\u01b0\u1ee3c li\u1ec7t k\u00ea ngay d\u01b0\u1edbi \u0111\u00e2y. Ch\u00fang ta c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng c\u00fa ph\u00e1p <code>__proto__<\/code> \u0111\u1ec3 minh h\u1ecda.<\/p>\n<blockquote><p>M\u1ed9t l\u01b0u \u00fd r\u1eb1ng c\u00fa ph\u00e1p <code>{ __proto__: ... }<\/code> kh\u00e1c v\u1edbi ph\u01b0\u01a1ng th\u1ee9c getter <code>obj.__proto__<\/code> v\u00e0 c\u00fa ph\u00e1p <code>{ __proto__: ... }<\/code> m\u1edbi l\u00e0 ti\u00eau chu\u1ea9n v\u00e0 \u0111\u01b0\u1ee3c khuy\u1ebfn kh\u00edch d\u00f9ng.<\/p><\/blockquote>\n<p>Trong m\u1ed9t object nh\u01b0 <code>{ a: 1, b: 2, __proto__: c }<\/code>, gi\u00e1 tr\u1ecb <code>c<\/code> (ph\u1ea3i l\u00e0 <code>null<\/code> ho\u1eb7c object kh\u00e1c) s\u1ebd tr\u1edf th\u00e0nh <code>[[Prototype]]<\/code> c\u1ee7a object \u0111\u01b0\u1ee3c bi\u1ec3u th\u1ecb b\u1eb1ng ch\u1eef, trong khi c\u00e1c key kh\u00e1c nh\u01b0 <code>a<\/code> v\u00e0 <code>b<\/code> s\u1ebd tr\u1edf th\u00e0nh <i>thu\u1ed9c t\u00ednh ri\u00eang<\/i> c\u1ee7a object. C\u00fa ph\u00e1p n\u00e0y \u0111\u1ecdc r\u1ea5t t\u1ef1 nhi\u00ean, v\u00ec <code>[[Prototype]]<\/code> ch\u1ec9 l\u00e0 m\u1ed9t &#8220;thu\u1ed9c t\u00ednh b\u00ean trong&#8221;.<\/p>\n<p>H\u00e3y xem v\u00ed d\u1ee5 v\u1ec1 truy c\u1eadp thu\u1ed9c t\u00ednh b\u00ean d\u01b0\u1edbi:<\/p>\n<pre>const o = {\r\n\r\na: 1,\r\n\r\nb: 2,\r\n\r\n\/\/ __proto__ thi\u1ebft l\u1eadp [[Prototype]]. N\u00f3 \u0111\u01b0\u1ee3c x\u00e1c \u0111\u1ecbnh\r\n\r\n\/\/ nh\u01b0 m\u1ed9t object kh\u00e1c.\r\n\r\n__proto__: {\r\n\r\nb: 3,\r\n\r\nc: 4,\r\n\r\n},\r\n\r\n};\r\n\r\n\r\n\/\/ o.[[Prototype]] c\u00f3 thu\u1ed9c t\u00ednh b v\u00e0 c.\r\n\r\n\/\/ o.[[Prototype]].[[Prototype]] l\u00e0 Object.prototype (ch\u00fang ta s\u1ebd gi\u1ea3i th\u00edch\r\n\r\n\/\/ \u00fd ngh\u0129a sau).\r\n\r\n\/\/ Cu\u1ed1i c\u00f9ng, o.[[Prototype]].[[Prototype]].[[Prototype]] l\u00e0 null.\r\n\r\n\/\/ \u0110\u00e2y l\u00e0 k\u1ebft th\u00fac c\u1ee7a prototype chain, l\u00e0 null,\r\n\r\n\/\/ theo \u0111\u1ecbnh ngh\u0129a, kh\u00f4ng s\u1edf h\u1eefu [[Prototype]].\r\n\r\n\/\/ Do \u0111\u00f3, to\u00e0n b\u1ed9 prototype chain s\u1ebd nh\u01b0 sau:\r\n\r\n\/\/ { a: 1, b: 2 } ---&gt; { b: 3, c: 4 } ---&gt; Object.prototype ---&gt; null\r\n\r\n\r\nconsole.log(o.a); \/\/ 1\r\n\r\n\/\/ C\u00f3 m\u1ed9t thu\u1ed9c t\u00ednh 'a' ri\u00eang tr\u00ean o kh\u00f4ng? C\u00f3, gi\u00e1 tr\u1ecb l\u00e0 1.\r\n\r\n\r\nconsole.log(o.b); \/\/ 2\r\n\r\n\/\/ C\u00f3 m\u1ed9t thu\u1ed9c t\u00ednh 'b' ri\u00eang tr\u00ean o kh\u00f4ng? C\u00f3, gi\u00e1 tr\u1ecb l\u00e0 2.\r\n\r\n\/\/ Prototype c\u0169ng c\u00f3 thu\u1ed9c t\u00ednh 'b', nh\u01b0ng n\u00f3 kh\u00f4ng \u0111\u01b0\u1ee3c truy c\u1eadp\r\n\r\n\/\/ \u0110\u00e2y \u0111\u01b0\u1ee3c g\u1ecdi l\u00e0 Property Shadowing\r\n\r\n\r\nconsole.log(o.c); \/\/ 4\r\n\r\n\/\/ C\u00f3 m\u1ed9t thu\u1ed9c t\u00ednh 'c' ri\u00eang tr\u00ean o kh\u00f4ng? Kh\u00f4ng, ki\u1ec3m tra prototype.\r\n\r\n\/\/ C\u00f3 m\u1ed9t thu\u1ed9c t\u00ednh 'c' ri\u00eang tr\u00ean o.[[Prototype]kh\u00f4ng]? C\u00f3, gi\u00e1 tr\u1ecb l\u00e0 4.\r\n\r\n\r\nconsole.log(o.d); \/\/ kh\u00f4ng x\u00e1c \u0111\u1ecbnh\r\n\r\n\/\/ C\u00f3 m\u1ed9t thu\u1ed9c t\u00ednh 'd' ri\u00eang tr\u00ean o kh\u00f4ng? Kh\u00f4ng, ki\u1ec3m tra prototype.\r\n\r\n\/\/ C\u00f3 m\u1ed9t thu\u1ed9c t\u00ednh 'd' ri\u00eang tr\u00ean o.[[Prototype]kh\u00f4ng]? Kh\u00f4ng, ki\u1ec3m tra prototype.\r\n\r\n\/\/ o.[[Prototype]].[[Prototype]] l\u00e0 Object.prototype v\u00e0\r\n\r\n\/\/ kh\u00f4ng c\u00f3 thu\u1ed9c t\u00ednh 'd' m\u1eb7c \u0111\u1ecbnh, ki\u1ec3m tra prototype.\r\n\r\n\/\/ o.[[Prototype]].[[Prototype]].[[Prototype]] l\u00e0 null, ng\u1eebng t\u00ecm ki\u1ebfm,\r\n\r\n\/\/ kh\u00f4ng t\u00ecm th\u1ea5y thu\u1ed9c t\u00ednh, tr\u1edf l\u1ea1i kh\u00f4ng x\u00e1c \u0111\u1ecbnh.<\/pre>\n<p>\u0110\u1eb7t thu\u1ed9c t\u00ednh cho object s\u1ebd t\u1ea1o thu\u1ed9c t\u00ednh ri\u00eang. Ngo\u1ea1i l\u1ec7 duy nh\u1ea5t l\u00e0 khi n\u00f3 b\u1ecb ch\u1eb7n b\u1edfi m\u1ed9t tr\u00ecnh thu th\u1eadp (getter) ho\u1eb7c tr\u00ecnh thi\u1ebft l\u1eadp (setter).<\/p>\n<p>T\u01b0\u01a1ng t\u1ef1, b\u1ea1n c\u00f3 th\u1ec3 t\u1ea1o n\u00ean m\u1ed9t prototype chain d\u00e0i h\u01a1n:<\/p>\n<pre>const o = {\r\n\r\na: 1,\r\n\r\nb: 2,\r\n\r\n\/\/ __proto__ thi\u1ebft l\u1eadp [[Prototype]]. N\u00f3 \u0111\u01b0\u1ee3c x\u00e1c \u0111\u1ecbnh\r\n\r\n\/\/ nh\u01b0 m\u1ed9t object kh\u00e1c.\r\n\r\n__proto__: {\r\n\r\nb: 3,\r\n\r\nc: 4,\r\n\r\n__proto__: {\r\n\r\nd: 5,\r\n\r\n},\r\n\r\n},\r\n\r\n};\r\n\r\n\r\n\r\n\r\n\/\/ { a: 1, b: 2 } ---&gt; { b: 3, c: 4 } ---&gt; { d: 5 } ---&gt; Object.prototype ---&gt; null<\/pre>\n<h3><strong>2. &#8220;Ph\u01b0\u01a1ng th\u1ee9c&#8221; k\u1ebf th\u1eeba<\/strong><\/h3>\n<p>JavaScript kh\u00f4ng c\u00f3 &#8220;ph\u01b0\u01a1ng th\u1ee9c&#8221; n\u00e0o theo ngh\u0129a \u201cph\u01b0\u01a1ng th\u1ee9c&#8221; m\u00e0 c\u00e1c ng\u00f4n ng\u1eef class-based th\u01b0\u1eddng \u0111\u1ecbnh ngh\u0129a. M\u00e0 trong JavaScript, b\u1ea5t k\u1ef3 ch\u1ee9c n\u0103ng n\u00e0o c\u0169ng c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c th\u00eam v\u00e0o m\u1ed9t object d\u01b0\u1edbi d\u1ea1ng thu\u1ed9c t\u00ednh.<\/p>\n<p>M\u1ed9t h\u00e0m \u0111\u01b0\u1ee3c k\u1ebf th\u1eeba ho\u1ea1t \u0111\u1ed9ng gi\u1ed1ng nh\u01b0 b\u1ea5t k\u1ef3 thu\u1ed9c t\u00ednh n\u00e0o kh\u00e1c, bao g\u1ed3m c\u1ea3 h\u00e0m properties shadowing nh\u01b0 \u0111\u01b0\u1ee3c hi\u1ec3n th\u1ecb \u1edf tr\u00ean (trong tr\u01b0\u1eddng h\u1ee3p n\u00e0y l\u00e0 m\u1ed9t d\u1ea1ng ph\u01b0\u01a1ng th\u1ee9c ghi \u0111\u00e8 &#8211; method overriding). Khi m\u1ed9t h\u00e0m k\u1ebf th\u1eeba \u0111\u01b0\u1ee3c th\u1ef1c thi, gi\u00e1 tr\u1ecb c\u1ee7a this tr\u1ecf \u0111\u1ebfn object k\u1ebf th\u1eeba, kh\u00f4ng ph\u1ea3i object prototype trong \u0111\u00f3 h\u00e0m l\u00e0 m\u1ed9t thu\u1ed9c t\u00ednh ri\u00eang.<\/p>\n<pre>const parent = {\r\n\r\n\u00a0\u00a0value: 2,\r\n\r\n\u00a0\u00a0method() {\r\n\r\n\u00a0\u00a0\u00a0\u00a0return this.value + 1;\r\n\r\n\u00a0\u00a0},\r\n\r\n};\r\n\r\n\r\nconsole.log(parent.method()); \/\/ 3\r\n\r\n\/\/ Khi g\u1ecdi parent.method trong tr\u01b0\u1eddng h\u1ee3p n\u00e0y, 'this' \u0111\u1ec3 ch\u1ec9 parent\r\n\r\n\r\n\/\/ child l\u00e0 m\u1ed9t object th\u1eeba k\u1ebf t\u1eeb parent\r\n\r\nconst child = {\r\n\r\n\u00a0\u00a0__proto__: parent,\r\n\r\n};\r\n\r\nconsole.log(child.method()); \/\/ 3\r\n\r\n\/\/ Khi g\u1ecdi child.method, 'this' \u0111\u1ec3 ch\u1ec9 child.\r\n\r\n\/\/ V\u1eady khi child k\u1ebf th\u1eeba ph\u01b0\u01a1ng th\u1ee9c t\u1eeb parent,\r\n\r\n\/\/ Thu\u1ed9c t\u00ednh 'value' s\u1ebd \u0111\u01b0\u1ee3c t\u00ecm tr\u00ean child. Tuy nhi\u00ean, v\u00ec child\r\n\r\n\/\/ kh\u00f4ng c\u00f3 thu\u1ed9c t\u00ednh ri\u00eang n\u00e0o l\u00e0 'value', thu\u1ed9c t\u00ednh s\u1ebd\r\n\r\n\/\/ \u0111\u01b0\u1ee3c t\u00ecm tr\u00ean [[Prototype]], ch\u00ednh l\u00e0 parent.value.\r\n\r\n\r\nchild.value = 4; \/\/ g\u00e1n gi\u00e1 tr\u1ecb 4 cho thu\u1ed9c t\u00ednh 'value' tr\u00ean child.\r\n\r\n\/\/ \u0110i\u1ec1u n\u00e0y s\u1ebd shadows thu\u1ed9c t\u00ednh 'value' tr\u00ean parent.\r\n\r\n\/\/ Object child s\u1ebd nh\u01b0 sau:\r\n\r\n\/\/ { value: 4, __proto__: { value: 2, method: [Function] } }\r\n\r\nconsole.log(child.method()); \/\/ 5\r\n\r\n\/\/ V\u00ec child \u0111\u00e3 c\u00f3 thu\u1ed9c t\u00ednh 'value', 'this.value' s\u1ebd ch\u1ec9\r\n\r\n\/\/ child.value<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Tong_ket\"><\/span><strong>T\u1ed5ng k\u1ebft<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Nh\u00ecn chung, kh\u00f4ng ph\u1ea3i l\u00fac n\u00e0o b\u1ea1n c\u0169ng c\u1ea7n ph\u1ea3i \u00e1p d\u1ee5ng prototype chain khi tri\u1ec3n khai JavaScript. Tuy nhi\u00ean, \u0111i\u1ec1u quan tr\u1ecdng l\u00e0 b\u1ea1n n\u00ean hi\u1ec3u c\u00e1ch th\u1ee9c ho\u1ea1t \u0111\u1ed9ng c\u1ee7a prototype chain v\u00ec \u0111\u00e2y l\u00e0 n\u1ec1n t\u1ea3ng cho t\u1eeb kh\u00f3a &#8220;class&#8221; v\u00e0 l\u00e0 ki\u1ebfn th\u1ee9c c\u1ea7n thi\u1ebft khi b\u1ea1n c\u00f3 nhu c\u1ea7u t\u00ecm hi\u1ec3u s\u00e2u h\u01a1n v\u1ec1 ng\u00f4n ng\u1eef JavaScript.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" class=\"sp-no-webp wp-image-5668 aligncenter sp-no-webp entered lazyloaded\" src=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2015\/07\/Robby2.png\" alt=\"robby-2\" width=\"300\" height=\"300\" data-lazy-src=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2015\/07\/Robby2.png\" data-lazy-sizes=\"(max-width: 300px) 100vw, 300px\" data-lazy-srcset=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2015\/07\/Robby2.png 300w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2015\/07\/Robby2-200x200.png 200w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2015\/07\/Robby2-60x60.png 60w\" data-ll-status=\"loaded\" srcset=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2015\/07\/Robby2.png 300w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2015\/07\/Robby2-200x200.png 200w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2015\/07\/Robby2-60x60.png 60w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/figure>\n<\/div>\n<p id=\"block-bdf220a1-3635-4794-9101-81d879939b92\" class=\"has-text-align-center\" style=\"text-align: center;\"><strong>B\u1ea1n th\u1ea5y b\u00e0i vi\u1ebft hay v\u00e0 h\u1eefu \u00edch? \u0110\u1eebng ng\u1ea1i Share v\u1edbi b\u1ea1n b\u00e8 v\u00e0 \u0111\u1ed3ng nghi\u1ec7p nh\u00e9.<\/strong><\/p>\n<p id=\"block-a1315aab-406f-48df-97fc-6b9584e6e134\" class=\"has-text-align-center\" style=\"text-align: center;\"><strong>V\u00e0 nhanh tay tham kh\u1ea3o\u00a0<a href=\"http:\/\/itviec.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">vi\u1ec7c l\u00e0m IT<\/a>\u00a0\u201cch\u1ea5t\u201d tr\u00ean ITviec<\/strong>!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Prototype chain l\u00e0 m\u1ed9t kh\u00e1i ni\u1ec7m quan tr\u1ecdng khi b\u1ea1n t\u00ecm hi\u1ec3u v\u1ec1 prototype trong JavaScript. \u0110\u1ecdc b\u00e0i vi\u1ebft n\u00e0y \u0111\u1ec3 hi\u1ec3u prototype chain l\u00e0 g\u00ec, t\u00ednh k\u1ebf th\u1eeba trong prototype chain th\u1ec3 hi\u1ec7n nh\u01b0 th\u1ebf n\u00e0o. T\u1eeb \u0111\u00f3, n\u1eafm v\u1eefng l\u00fd thuy\u1ebft v\u00e0 c\u00e1ch \u00e1p d\u1ee5ng prototype chain. Prototype chain l\u00e0 g\u00ec? Trong JavaScript, [&hellip;]<\/p>\n","protected":false},"author":95,"featured_media":40917,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_gspb_post_css":"","footnotes":""},"categories":[109],"tags":[],"class_list":["post-39549","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-chuyen-mon-it"],"blocksy_meta":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.8 (Yoast SEO v27.8) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Prototype chain l\u00e0 g\u00ec? - ITviec Blog<\/title>\n<meta name=\"description\" content=\"\u0110\u1ecdc b\u00e0i vi\u1ebft n\u00e0y \u0111\u1ec3 hi\u1ec3u prototype chain l\u00e0 g\u00ec v\u00e0 th\u1ec3 hi\u1ec7n nh\u01b0 th\u1ebf n\u00e0o. T\u1eeb \u0111\u00f3, n\u1eafm v\u1eefng l\u00fd thuy\u1ebft v\u00e0 c\u00e1ch \u00e1p d\u1ee5ng prototype chain.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/itviec.com\/blog\/prototype-chain-la-gi\/\" \/>\n<meta property=\"og:locale\" content=\"vi_VN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Prototype chain l\u00e0 g\u00ec?\" \/>\n<meta property=\"og:description\" content=\"Prototype chain l\u00e0 m\u1ed9t kh\u00e1i ni\u1ec7m quan tr\u1ecdng khi b\u1ea1n t\u00ecm hi\u1ec3u v\u1ec1 prototype trong JavaScript. \u0110\u1ecdc b\u00e0i vi\u1ebft n\u00e0y \u0111\u1ec3 hi\u1ec3u prototype chain l\u00e0 g\u00ec, t\u00ednh k\u1ebf th\u1eeba\" \/>\n<meta property=\"og:url\" content=\"https:\/\/itviec.com\/blog\/prototype-chain-la-gi\/\" \/>\n<meta property=\"og:site_name\" content=\"ITviec Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ITviec\" \/>\n<meta property=\"article:published_time\" content=\"2023-09-15T05:08:14+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2023\/09\/prototype-chain-la-gi-blog-vippro.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"950\" \/>\n\t<meta property=\"og:image:height\" content=\"500\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Tuong Uyen\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ITviec\" \/>\n<meta name=\"twitter:site\" content=\"@ITviec\" \/>\n<meta name=\"twitter:label1\" content=\"\u0110\u01b0\u1ee3c vi\u1ebft b\u1edfi\" \/>\n\t<meta name=\"twitter:data1\" content=\"Tuong Uyen\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u01af\u1edbc t\u00ednh th\u1eddi gian \u0111\u1ecdc\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 ph\u00fat\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Prototype chain l\u00e0 g\u00ec? - ITviec Blog","description":"\u0110\u1ecdc b\u00e0i vi\u1ebft n\u00e0y \u0111\u1ec3 hi\u1ec3u prototype chain l\u00e0 g\u00ec v\u00e0 th\u1ec3 hi\u1ec7n nh\u01b0 th\u1ebf n\u00e0o. T\u1eeb \u0111\u00f3, n\u1eafm v\u1eefng l\u00fd thuy\u1ebft v\u00e0 c\u00e1ch \u00e1p d\u1ee5ng prototype chain.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/itviec.com\/blog\/prototype-chain-la-gi\/","og_locale":"vi_VN","og_type":"article","og_title":"Prototype chain l\u00e0 g\u00ec?","og_description":"Prototype chain l\u00e0 m\u1ed9t kh\u00e1i ni\u1ec7m quan tr\u1ecdng khi b\u1ea1n t\u00ecm hi\u1ec3u v\u1ec1 prototype trong JavaScript. \u0110\u1ecdc b\u00e0i vi\u1ebft n\u00e0y \u0111\u1ec3 hi\u1ec3u prototype chain l\u00e0 g\u00ec, t\u00ednh k\u1ebf th\u1eeba","og_url":"https:\/\/itviec.com\/blog\/prototype-chain-la-gi\/","og_site_name":"ITviec Blog","article_publisher":"https:\/\/www.facebook.com\/ITviec","article_published_time":"2023-09-15T05:08:14+00:00","og_image":[{"width":950,"height":500,"url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2023\/09\/prototype-chain-la-gi-blog-vippro.jpg","type":"image\/jpeg"}],"author":"Tuong Uyen","twitter_card":"summary_large_image","twitter_creator":"@ITviec","twitter_site":"@ITviec","twitter_misc":{"\u0110\u01b0\u1ee3c vi\u1ebft b\u1edfi":"Tuong Uyen","\u01af\u1edbc t\u00ednh th\u1eddi gian \u0111\u1ecdc":"4 ph\u00fat"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/itviec.com\/blog\/prototype-chain-la-gi\/#article","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/prototype-chain-la-gi\/"},"author":{"name":"Tuong Uyen","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/e97d0e359f8840eaea7dc3a96006a8d4"},"headline":"Prototype chain l\u00e0 g\u00ec?","datePublished":"2023-09-15T05:08:14+00:00","mainEntityOfPage":{"@id":"https:\/\/itviec.com\/blog\/prototype-chain-la-gi\/"},"wordCount":1171,"publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"image":{"@id":"https:\/\/itviec.com\/blog\/prototype-chain-la-gi\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2023\/09\/prototype-chain-la-gi-blog-vippro.jpg","articleSection":["Chuy\u00ean m\u00f4n IT"],"inLanguage":"vi"},{"@type":"WebPage","@id":"https:\/\/itviec.com\/blog\/prototype-chain-la-gi\/","url":"https:\/\/itviec.com\/blog\/prototype-chain-la-gi\/","name":"Prototype chain l\u00e0 g\u00ec? - ITviec Blog","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/itviec.com\/blog\/prototype-chain-la-gi\/#primaryimage"},"image":{"@id":"https:\/\/itviec.com\/blog\/prototype-chain-la-gi\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2023\/09\/prototype-chain-la-gi-blog-vippro.jpg","datePublished":"2023-09-15T05:08:14+00:00","description":"\u0110\u1ecdc b\u00e0i vi\u1ebft n\u00e0y \u0111\u1ec3 hi\u1ec3u prototype chain l\u00e0 g\u00ec v\u00e0 th\u1ec3 hi\u1ec7n nh\u01b0 th\u1ebf n\u00e0o. T\u1eeb \u0111\u00f3, n\u1eafm v\u1eefng l\u00fd thuy\u1ebft v\u00e0 c\u00e1ch \u00e1p d\u1ee5ng prototype chain.","breadcrumb":{"@id":"https:\/\/itviec.com\/blog\/prototype-chain-la-gi\/#breadcrumb"},"inLanguage":"vi","potentialAction":[{"@type":"ReadAction","target":["https:\/\/itviec.com\/blog\/prototype-chain-la-gi\/"]}]},{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/prototype-chain-la-gi\/#primaryimage","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2023\/09\/prototype-chain-la-gi-blog-vippro.jpg","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2023\/09\/prototype-chain-la-gi-blog-vippro.jpg","width":950,"height":500,"caption":"prototype chain l\u00e0 g\u00ec - itviec blog"},{"@type":"BreadcrumbList","@id":"https:\/\/itviec.com\/blog\/prototype-chain-la-gi\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Chuy\u00ean m\u00f4n IT","item":"https:\/\/itviec.com\/blog\/chuyen-mon-it\/"},{"@type":"ListItem","position":2,"name":"Prototype chain l\u00e0 g\u00ec?"}]},{"@type":"WebSite","@id":"https:\/\/itviec.com\/blog\/#website","url":"https:\/\/itviec.com\/blog\/","name":"ITviec Blog","description":"IT Jobs &amp; People in Vietnam","publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/itviec.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"vi"},{"@type":"Organization","@id":"https:\/\/itviec.com\/blog\/#organization","name":"ITviec","url":"https:\/\/itviec.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2018\/12\/itviec-black-square-facebook.png","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2018\/12\/itviec-black-square-facebook.png","width":1800,"height":1800,"caption":"ITviec"},"image":{"@id":"https:\/\/itviec.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/ITviec","https:\/\/x.com\/ITviec","https:\/\/www.linkedin.com\/company\/itviec","https:\/\/www.youtube.com\/channel\/UCYthAQ3bcGr57M_ag5gHDvQ"]},{"@type":"Person","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/e97d0e359f8840eaea7dc3a96006a8d4","name":"Tuong Uyen","image":{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2022\/10\/tuong-uyen-profile-picture-100x100.jpg","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2022\/10\/tuong-uyen-profile-picture-100x100.jpg","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2022\/10\/tuong-uyen-profile-picture-100x100.jpg","caption":"Tuong Uyen"},"url":"https:\/\/itviec.com\/blog\/author\/tuong-uyen-pikachu\/"}]}},"_links":{"self":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/39549","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/users\/95"}],"replies":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/comments?post=39549"}],"version-history":[{"count":0,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/39549\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media\/40917"}],"wp:attachment":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media?parent=39549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/categories?post=39549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/tags?post=39549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}