{"id":39546,"date":"2023-08-17T16:15:43","date_gmt":"2023-08-17T09:15:43","guid":{"rendered":"https:\/\/itviec.com\/blog\/?p=39546"},"modified":"2023-08-17T16:15:43","modified_gmt":"2023-08-17T09:15:43","slug":"prototype-trong-javascript-la-gi","status":"publish","type":"post","link":"https:\/\/itviec.com\/blog\/prototype-trong-javascript-la-gi\/","title":{"rendered":"Prototype trong Javascript: \u0110\u1ecbnh ngh\u0129a, Ph\u00e2n lo\u1ea1i v\u00e0 C\u00e1ch s\u1eed d\u1ee5ng"},"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-trong-javascript-la-gi\/#Prototype_la_gi\" >Prototype 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-trong-javascript-la-gi\/#Cong_dung_cua_prototype_la_gi\" >C\u00f4ng d\u1ee5ng c\u1ee7a prototype 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-3\" href=\"https:\/\/itviec.com\/blog\/prototype-trong-javascript-la-gi\/#Loi_ich_cua_prototype_la_gi\" >L\u1ee3i \u00edch c\u1ee7a prototype 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-4\" href=\"https:\/\/itviec.com\/blog\/prototype-trong-javascript-la-gi\/#Cach_tao_prototype\" >C\u00e1ch t\u1ea1o prototype<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/itviec.com\/blog\/prototype-trong-javascript-la-gi\/#Cach_su_dung_va_thao_tac_voi_prototype\" >C\u00e1ch s\u1eed d\u1ee5ng v\u00e0 thao t\u00e1c v\u1edbi prototype<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/itviec.com\/blog\/prototype-trong-javascript-la-gi\/#Moi_quan_he_giua_tinh_ke_thua_va_prototype_la_gi\" >M\u1ed1i quan h\u1ec7 gi\u1eefa t\u00ednh k\u1ebf th\u1eeba  v\u00e0 prototype 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-7\" href=\"https:\/\/itviec.com\/blog\/prototype-trong-javascript-la-gi\/#Nhung_cau_hoi_thuong_gap_ve_prototype_trong_Javascript\" >Nh\u1eefng c\u00e2u h\u1ecfi th\u01b0\u1eddng g\u1eb7p v\u1ec1 prototype trong Javascript<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/itviec.com\/blog\/prototype-trong-javascript-la-gi\/#Tong_ket\" >T\u1ed5ng k\u1ebft<\/a><\/li><\/ul><\/nav><\/div>\n<p><b><i>\u0110\u1ed1i v\u1edbi m\u1ed9t ng\u00f4n ng\u1eef prototype-based nh\u01b0 JavaScript, prototype l\u00e0 m\u1ed9t kh\u00e1i ni\u1ec7m c\u1ed1t l\u00f5i m\u00e0 b\u1ea5t c\u1ee9 l\u1eadp tr\u00ecnh vi\u00ean JavaScript n\u00e0o c\u0169ng c\u1ea7n bi\u1ebft. H\u00e3y c\u00f9ng \u0111i qua nh\u1eefng ki\u1ebfn th\u1ee9c t\u1eeb c\u01a1 b\u1ea3n nh\u01b0 prototype l\u00e0 g\u00ec cho \u0111\u1ebfn c\u1ea7n thi\u1ebft nh\u1ea5t v\u1ec1 c\u00e1ch s\u1eed d\u1ee5ng prototype v\u00e0 \u00e1p d\u1ee5ng ch\u00fang hi\u1ec7u qu\u1ea3 v\u00e0o d\u1ef1 \u00e1n c\u1ee7a b\u1ea1n.<\/i><\/b><\/p>\n<p>\u0110\u1ecdc b\u00e0i vi\u1ebft n\u00e0y b\u1ea1n s\u1ebd d\u1ec5 d\u00e0ng n\u1eafm \u0111\u01b0\u1ee3c:<\/p>\n<ul>\n<li aria-level=\"1\">Prototype l\u00e0 g\u00ec trong JavaScript v\u00e0 nh\u1eefng thu\u1eadt ng\u1eef c\u01a1 b\u1ea3n<\/li>\n<li aria-level=\"1\">Hi\u1ec3u r\u00f5 c\u01a1 ch\u1ebf v\u00e0 c\u00e1c thao t\u00e1c c\u1ea7n thi\u1ebft \u0111\u1ec3 s\u1eed d\u1ee5ng prototype hi\u1ec7u qu\u1ea3<\/li>\n<li aria-level=\"1\">N\u1eafm r\u00f5 ki\u1ebfn th\u1ee9c v\u1ec1 t\u00ednh k\u1ebf th\u1eeba c\u1ee7a prototype<\/li>\n<\/ul>\n<p>Tr\u01b0\u1edbc khi \u0111\u1ecdc b\u00e0i vi\u1ebft n\u00e0y, b\u1ea1n n\u00ean \u0111\u1ecdc b\u00e0i vi\u1ebft <a href=\"https:\/\/itviec.com\/blog\/javascript-la-gi\/\" target=\"_blank\" rel=\"noopener\"><strong>T\u1ed5ng quan JavaScript A-Z<\/strong><\/a> \u0111\u1ec3 n\u1eafm r\u00f5 ki\u1ebfn th\u1ee9c c\u01a1 b\u1ea3n nh\u1ea5t v\u1ec1 ng\u00f4n ng\u1eef l\u1eadp tr\u00ecnh n\u00e0y.<\/p>\n<blockquote><p><em><a href=\"https:\/\/itviec.com\/it-jobs\/javascript\" target=\"_blank\" rel=\"noopener\"><strong>Vi\u1ec7c l\u00e0m JavaScript<\/strong><\/a> to\u00e0n qu\u1ed1c tr\u00ean ITviec<\/em><\/p><\/blockquote>\n<h2><span class=\"ez-toc-section\" id=\"Prototype_la_gi\"><\/span><b>Prototype l\u00e0 g\u00ec?<br \/>\n<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><b> Prototype l\u00e0 g\u00ec?<\/b><\/h3>\n<p>V\u1edbi JavaScript, prototype d\u00f9ng \u0111\u1ec3 ch\u1ec9 m\u1ed9t c\u01a1 ch\u1ebf cho ph\u00e9p c\u00e1c object k\u1ebf th\u1eeba c\u00e1c thu\u1ed9c t\u00ednh (properties) v\u00e0 ph\u01b0\u01a1ng th\u1ee9c (methods) t\u1eeb c\u00e1c object kh\u00e1c.<\/p>\n<blockquote><p><em>Tham kh\u1ea3o: <strong><a href=\"https:\/\/itviec.com\/blog\/tai-lieu-javascript\/\" target=\"_blank\" rel=\"noopener\">H\u1ecdc JavaScript<\/a> v\u1edbi 20+ t\u00e0i li\u1ec7u JavaScript &#8220;ch\u1ea5t&#8221; nh\u1ea5t<\/strong><\/em><\/p><\/blockquote>\n<p>Theo \u0111\u00f3, b\u1ea1n s\u1ebd g\u1eb7p 2 kh\u00e1i ni\u1ec7m l\u00e0 <i>prototype c\u1ee7a h\u00e0m<\/i> v\u00e0 <i>prototype c\u1ee7a object<\/i>. \u0110\u1ecbnh ngh\u0129a c\u1ee5 th\u1ec3 c\u1ee7a hai kh\u00e1i ni\u1ec7m \u0111\u00f3 nh\u01b0 sau:<\/p>\n<ul>\n<li aria-level=\"1\">Prototype c\u1ee7a h\u00e0m: Prototype c\u1ee7a h\u00e0m l\u00e0 <i>object instance<\/i> m\u00e0 s\u1ebd tr\u1edf th\u00e0nh prototype cho t\u1ea5t c\u1ea3 c\u00e1c object \u0111\u01b0\u1ee3c t\u1ea1o b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng h\u00e0m n\u00e0y l\u00e0m constructor.<\/li>\n<li aria-level=\"1\">Prototype c\u1ee7a object: Prototype c\u1ee7a object l\u00e0 <i>object instance<\/i> m\u00e0 object \u0111\u01b0\u1ee3c k\u1ebf th\u1eeba t\u1eeb \u0111\u00f3.<\/li>\n<\/ul>\n<p>C\u00f3 th\u1ec3 n\u00f3i, prototype l\u00e0 m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng \u0111\u01b0\u1ee3c li\u00ean k\u1ebft v\u1edbi m\u1ecdi h\u00e0m v\u00e0 object theo m\u1eb7c \u0111\u1ecbnh trong JavaScript. Trong \u0111\u00f3, b\u1ea1n c\u00f3 th\u1ec3 truy c\u1eadp v\u00e0 s\u1eeda \u0111\u1ed5i \u0111\u01b0\u1ee3c thu\u1ed9c t\u00ednh <i>prototype c\u1ee7a h\u00e0m<\/i>. C\u00f2n thu\u1ed9c t\u00ednh <i>prototype c\u1ee7a object<\/i> (c\u00f2n g\u1ecdi l\u00e0 attribute) s\u1ebd kh\u00f4ng \u0111\u01b0\u1ee3c hi\u1ec3n th\u1ecb.<\/p>\n<h3><b>Prototype c\u1ee7a h\u00e0m l\u00e0 g\u00ec?<\/b><\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-39731 size-full\" src=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2023\/08\/prototype-javascript-2-vippro.png\" alt=\"prototype javascript -prototype l\u00e0 g\u00ec - itviec\" width=\"2084\" height=\"648\" srcset=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2023\/08\/prototype-javascript-2-vippro.png 2084w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2023\/08\/prototype-javascript-2-vippro-300x93.png 300w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2023\/08\/prototype-javascript-2-vippro-640x199.png 640w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2023\/08\/prototype-javascript-2-vippro-200x62.png 200w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2023\/08\/prototype-javascript-2-vippro-768x239.png 768w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2023\/08\/prototype-javascript-2-vippro-1536x478.png 1536w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2023\/08\/prototype-javascript-2-vippro-2048x637.png 2048w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2023\/08\/prototype-javascript-2-vippro-100x31.png 100w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2023\/08\/prototype-javascript-2-vippro-700x218.png 700w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2023\/08\/prototype-javascript-2-vippro-1600x498.png 1600w\" sizes=\"auto, (max-width: 2084px) 100vw, 2084px\" \/><\/p>\n<p style=\"text-align: center;\"><em>\u1ea2nh m\u1eabu: @TutorialsTeacher.com<\/em><\/p>\n<p>Trong JavaScript, t\u1ea5t c\u1ea3 c\u00e1c h\u00e0m \u0111\u1ec1u c\u00f3 thu\u1ed9c t\u00ednh prototype. Thu\u1ed9c t\u00ednh prototype \u0111\u00f3 t\u01b0\u01a1ng t\u1ef1 nh\u01b0 m\u1ed9t class definition trong c\u00e1c ng\u00f4n ng\u1eef h\u01b0\u1edbng \u0111\u1ed1i t\u01b0\u1ee3ng kh\u00e1c, nh\u01b0ng \u0111\u1ec3 \u0111\u1ecbnh ngh\u0129a ch\u00ednh x\u00e1c h\u01a1n, prototype c\u1ee7a h\u00e0m l\u00e0 m\u1ed9t object instance.<\/p>\n<p>M\u1ecdi h\u00e0m trong JavaScript \u0111\u1ec1u c\u00f3 m\u1ed9t <b>prototype <\/b>object cho d\u00f9 b\u1ea1n c\u00f3 s\u1eed d\u1ee5ng hay kh\u00f4ng.<\/p>\n<p>Sau khi b\u1ea1n t\u1ea1o m\u1ed9t object m\u1edbi b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng t\u1eeb kh\u00f3a <i>new<\/i>, chuy\u1ec3n object m\u1edbi d\u01b0\u1edbi d\u1ea1ng <i>this<\/i> t\u1edbi h\u00e0m constructor. Sau \u0111\u00f3, h\u00e0m constructor l\u1ea5y object instance (m\u00e0 \u0111\u01b0\u1ee3c tr\u1ecf t\u1edbi b\u1edfi thu\u1ed9c t\u00ednh prototype c\u1ee7a h\u00e0m \u0111\u00f3) v\u00e0 g\u00e1n object instance l\u00e0m prototype cho object \u0111\u00f3.<\/p>\n<h3><b> Prototype c\u1ee7a object l\u00e0 g\u00ec?<\/b><\/h3>\n<p>Object kh\u00f4ng c\u00f3 thu\u1ed9c t\u00ednh prototype, nh\u01b0ng ch\u00fang c\u00f3 prototype.<\/p>\n<p>Prototype c\u1ee7a m\u1ed9t object l\u00e0 m\u1ed9t object m\u00e0 k\u1ebf th\u1eeba c\u00e1c thu\u1ed9c t\u00ednh t\u1eeb object \u0111\u00f3. L\u01b0u \u00fd, k\u1ebf th\u1eeba thu\u1ed9c t\u00ednh, kh\u00f4ng ph\u1ea3i k\u1ebf th\u1eeba h\u00e0m hay class. \u0110i\u1ec1u n\u00e0y kh\u00e1c v\u1edbi prototype c\u1ee7a h\u00e0m v\u00ec prototype c\u1ee7a h\u00e0m \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng l\u00e0m object \u0111\u1ec3 \u0111\u01b0\u1ee3c ch\u1ec9 \u0111\u1ecbnh l\u00e0m prototype cho c\u00e1c object m\u1edbi m\u00e0 ch\u00fang \u0111\u01b0\u1ee3c t\u1ea1o b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng h\u00e0m n\u00e0y l\u00e0m h\u00e0m t\u1ea1o.<\/p>\n<p>Tuy kh\u00e1c nhau v\u1ec1 m\u1eb7t kh\u00e1i ni\u1ec7m nh\u01b0ng tr\u00ean th\u1ef1c t\u1ebf, prototype c\u1ee7a m\u1ed9t h\u00e0m v\u00e0 prototype c\u1ee7a m\u1ed9t object th\u01b0\u1eddng tr\u1ecf \u0111\u1ebfn c\u00f9ng m\u1ed9t object trong b\u1ed9 nh\u1edb.<\/p>\n<p>C\u00f3 th\u1ec3 truy xu\u1ea5t prototype c\u1ee7a m\u1ed9t object b\u1eb1ng c\u00e1ch g\u1ecdi <code>myObject.__proto__<\/code> trong h\u1ea7u h\u1ebft c\u00e1c tr\u00ecnh duy\u1ec7t v\u00e0 g\u1ecdi <code>Object.getPrototypeOf(myObject)<\/code> trong t\u1ea5t c\u1ea3 c\u00e1c tr\u00ecnh duy\u1ec7t.<\/p>\n<h3><b> [[Prototype]] l\u00e0 g\u00ec?<\/b><\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-39732 size-full\" src=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2023\/08\/prototype-javascript-1-vippro.png\" alt=\"prototype javascript - prototype l\u00e0 g\u00ec - itviec\" width=\"1548\" height=\"1320\" srcset=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2023\/08\/prototype-javascript-1-vippro.png 1548w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2023\/08\/prototype-javascript-1-vippro-300x256.png 300w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2023\/08\/prototype-javascript-1-vippro-640x546.png 640w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2023\/08\/prototype-javascript-1-vippro-200x171.png 200w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2023\/08\/prototype-javascript-1-vippro-768x655.png 768w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2023\/08\/prototype-javascript-1-vippro-1536x1310.png 1536w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2023\/08\/prototype-javascript-1-vippro-100x85.png 100w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2023\/08\/prototype-javascript-1-vippro-528x450.png 528w, https:\/\/itviec.com\/blog\/wp-content\/uploads\/2023\/08\/prototype-javascript-1-vippro-1407x1200.png 1407w\" sizes=\"auto, (max-width: 1548px) 100vw, 1548px\" \/><\/p>\n<p>Trong JavaScript, c\u00e1c object \u0111\u1ec1u c\u00f3 m\u1ed9t thu\u1ed9c t\u00ednh <code>[[Prototype]]<\/code> \u1ea9n, m\u00e0 c\u00f3 th\u1ec3 l\u00e0 <code>null<\/code> ho\u1eb7c l\u00e0 tham chi\u1ebfu object kh\u00e1c. Object \u0111\u01b0\u1ee3c tham chi\u1ebfu b\u1edfi <code>[[Prototype]]<\/code> \u0111\u01b0\u1ee3c g\u1ecdi l\u00e0 m\u1ed9t \u201cprototype&#8221;.<\/p>\n<p>Ch\u00fang ta c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng <code>obj .__ proto__<\/code> \u0111\u1ec3 truy c\u1eadp v\u00e0o <code>[[Prototype]]<\/code>. \u0110\u00e2y l\u00e0 m\u1ed9t getter\/setter \u0111\u00e3 c\u0169 v\u00e0 c\u00f3 nh\u1eefng c\u00e1ch kh\u00e1c m\u00e0 s\u1ebd \u0111\u01b0\u1ee3c \u0111\u1ec1 c\u1eadp ngay trong b\u00e0i vi\u1ebft n\u00e0y.<\/p>\n<h3><b> __proto__ l\u00e0 g\u00ec?<\/b><\/h3>\n<p><code>__proto__<\/code> kh\u00f4ng gi\u1ed1ng nh\u01b0 thu\u1ed9c t\u00ednh <code>[[Prototype]]<\/code>. \u0110\u00e2y l\u00e0 m\u1ed9t getter\/setter cho <code>[[Prototype]]<\/code> nh\u01b0ng \u0111\u00e3 l\u1ed7i th\u1eddi.<\/p>\n<p>JavaScript hi\u1ec7n \u0111\u1ea1i khuy\u1ebfn kh\u00edch l\u1eadp tr\u00ecnh vi\u00ean n\u00ean s\u1eed d\u1ee5ng h\u00e0m <code>Object.getPrototypeOf\/Object.setPrototypeOf<\/code> thay v\u00ec get\/set prototype.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Cong_dung_cua_prototype_la_gi\"><\/span>C\u00f4ng d\u1ee5ng c\u1ee7a prototype l\u00e0 g\u00ec?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Nh\u01b0 \u0111\u00e3 tr\u00ecnh b\u00e0y, prototype cho ph\u00e9p b\u1ea1n d\u1ec5 d\u00e0ng x\u00e1c \u0111\u1ecbnh c\u00e1c ph\u01b0\u01a1ng th\u1ee9c cho t\u1ea5t c\u1ea3 c\u00e1c instance c\u1ee7a m\u1ed9t object c\u1ee5 th\u1ec3. C\u00e1i hay l\u00e0 ph\u01b0\u01a1ng th\u1ee9c \u0111\u01b0\u1ee3c \u00e1p d\u1ee5ng cho prototype, v\u00ec v\u1eady ch\u00fang ch\u1ec9 \u0111\u01b0\u1ee3c l\u01b0u trong b\u1ed9 nh\u1edb m\u1ed9t l\u1ea7n, nh\u01b0ng m\u1ecdi h\u00e0nh \u0111\u1ed9ng c\u1ee7a object \u0111\u1ec1u c\u00f3 quy\u1ec1n truy c\u1eadp v\u00e0o.<\/p>\n<p>H\u00e3y xem qua object Pet trong v\u00ed d\u1ee5 b\u00ean d\u01b0\u1edbi:<\/p>\n<pre>function Pet(name, species){\r\n\r\n\u00a0\u00a0\u00a0\u00a0this.name = name;\r\n\r\n\u00a0\u00a0\u00a0\u00a0this.species = species;\r\n\r\n}\r\n\r\nfunction view(){\r\n\r\n\u00a0\u00a0\u00a0\u00a0return this.name + \" is a \" + this.species + \"!\";\r\n\r\n}\r\n\r\nPet.prototype.view = view;\r\n\r\nvar pet1 = new Pet('Tom', 'Cat');\r\n\r\nalert(pet1.view()); \/\/\u0110\u1ea7u ra: \"Tom is a Cat!\"<\/pre>\n<p>T\u1eeb v\u00ed d\u1ee5 tr\u00ean, b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng prototype \u0111\u01a1n gi\u1ea3n, b\u1ea1n \u0111\u00e3 \u0111\u1ea3m b\u1ea3o r\u1eb1ng t\u1ea5t c\u1ea3 c\u00e1c object Pet \u0111\u1ec1u c\u00f3 quy\u1ec1n truy c\u1eadp v\u00e0o ph\u01b0\u01a1ng th\u1ee9c xem.<\/p>\n<p>B\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng ph\u01b0\u01a1ng ph\u00e1p prototype \u0111\u1ec3 l\u00e0m vi\u1ec7c hi\u1ec7u qu\u1ea3 h\u01a1n. V\u00ed d\u1ee5:<\/p>\n<blockquote><p>Gi\u1ea3 s\u1eed ch\u00fang ta mu\u1ed1n c\u00f3 m\u1ed9t object Cat. Object n\u00e0y s\u1ebd k\u1ebf th\u1eeba t\u1eebng ph\u01b0\u01a1ng th\u1ee9c v\u00e0 thu\u1ed9c t\u00ednh \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng trong object Pet v\u00e0 \u0111\u1ed3ng th\u1eddi c\u0169ng c\u00f3 th\u1ec3 t\u1ea1o m\u1ed9t ch\u1ee9c n\u0103ng \u0111\u1eb7c bi\u1ec7t m\u00e0 ch\u1ec9 object Cat m\u1edbi c\u00f3 quy\u1ec1n truy c\u1eadp.<\/p>\n<p>Prototype s\u1ebd gi\u00fap b\u1ea1n tri\u1ec3n khai m\u1ed9t c\u00e1ch d\u1ec5 d\u00e0ng.<\/p><\/blockquote>\n<h2><span class=\"ez-toc-section\" id=\"Loi_ich_cua_prototype_la_gi\"><\/span><strong>L\u1ee3i \u00edch c\u1ee7a prototype l\u00e0 g\u00ec?<br \/>\n<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>C\u01a1 ch\u1ebf prototype cho ph\u00e9p t\u00e1i s\u1eed d\u1ee5ng code v\u00e0 t\u1ea1o th\u00e0nh c\u01a1 s\u1edf k\u1ebf th\u1eeba object trong JavaScript. \u0110i\u1ec1u n\u00e0y kh\u00f4ng ch\u1ec9 l\u00e0m gi\u1ea3m s\u1ef1 d\u01b0 th\u1eeba m\u00e0 c\u00f2n g\u00f3p ph\u1ea7n qu\u1ea3n l\u00fd b\u1ed9 nh\u1edb hi\u1ec7u qu\u1ea3 h\u01a1n v\u00ec c\u00e1c ch\u1ee9c n\u0103ng \u0111\u01b0\u1ee3c chia s\u1ebb kh\u00f4ng \u0111\u01b0\u1ee3c sao ch\u00e9p cho t\u1eebng instance c\u1ee7a object. C\u1ee5 th\u1ec3 h\u01a1n:<\/p>\n<h3><b>K\u1ebf th\u1eeba object v\u00e0 kh\u1ea3 n\u0103ng t\u00e1i s\u1eed d\u1ee5ng code<\/b><\/h3>\n<p>Prototype l\u00e0 n\u1ec1n t\u1ea3ng \u0111\u1ec3 th\u1ef1c hi\u1ec7n k\u1ebf th\u1eeba object trong JavaScript. Khi t\u1ea1o c\u00e1c object, JavaScript s\u1ebd t\u1ef1 \u0111\u1ed9ng li\u00ean k\u1ebft c\u00e1c object v\u1edbi prototype c\u1ee7a ch\u00fang, t\u1ea1o th\u00e0nh m\u1ed9t prototype chain.<\/p>\n<blockquote><p><em>\u0110\u1ecdc th\u00eam: <strong><a href=\"https:\/\/itviec.com\/blog\/prototype-chain-la-gi\/\" target=\"_blank\" rel=\"noopener\">Prototype chain<\/a> l\u00e0 g\u00ec? T\u00ednh k\u1ebf th\u1eeba v\u1edbi prototype chain ho\u1ea1t \u0111\u1ed9ng nh\u01b0 th\u1ebf n\u00e0o?<\/strong><\/em><\/p><\/blockquote>\n<p>Chu\u1ed7i n\u00e0y cho ph\u00e9p c\u00e1c object k\u1ebf th\u1eeba c\u00e1c thu\u1ed9c t\u00ednh v\u00e0 ph\u01b0\u01a1ng th\u1ee9c t\u1eeb prototype c\u1ee7a ch\u00fang, t\u1ea1o ra m\u1ed1i quan h\u1ec7 ph\u00e2n c\u1ea5p. Kh\u1ea3 n\u0103ng k\u1ebf th\u1eeba c\u00e1c ch\u1ee9c n\u0103ng (functionality) th\u00fac \u0111\u1ea9y kh\u1ea3 n\u0103ng t\u00e1i s\u1eed d\u1ee5ng code, khi b\u1ea1n x\u00e1c \u0111\u1ecbnh c\u00e1c thu\u1ed9c t\u00ednh chung trong prototype.<\/p>\n<p>B\u1eb1ng c\u00e1ch t\u1eadn d\u1ee5ng c\u00e1c prototype, b\u1ea1n c\u00f3 th\u1ec3 code g\u1ecdn g\u00e0ng h\u01a1n, c\u00f3 t\u1ed5 ch\u1ee9c h\u01a1n. \u0110\u00e2y l\u00e0 m\u1ed9t t\u00ednh n\u0103ng m\u1ea1nh m\u1ebd cho ph\u00e9p c\u00e1c l\u1eadp tr\u00ecnh vi\u00ean x\u00e2y d\u1ef1ng c\u00e1c \u1ee9ng d\u1ee5ng ph\u1ee9c t\u1ea1p v\u00e0 c\u00f3 th\u1ec3 m\u1edf r\u1ed9ng \u1ee9ng d\u1ee5ng m\u1ed9t c\u00e1ch hi\u1ec7u qu\u1ea3.<\/p>\n<h3><b>Qu\u1ea3n l\u00fd b\u1ed9 nh\u1edb hi\u1ec7u qu\u1ea3<\/b><\/h3>\n<p>T\u1ed1i \u01b0u h\u00f3a b\u1ed9 nh\u1edb r\u1ea5t quan tr\u1ecdng \u0111\u1ed1i v\u1edbi c\u00e1c \u1ee9ng d\u1ee5ng hi\u1ec7u su\u1ea5t cao, \u0111\u1eb7c bi\u1ec7t l\u00e0 nh\u1eefng \u1ee9ng d\u1ee5ng x\u1eed l\u00fd c\u00e1c t\u1eadp d\u1eef li\u1ec7u l\u1edbn. Prototype \u0111\u00f3ng m\u1ed9t vai tr\u00f2 quan tr\u1ecdng trong vi\u1ec7c qu\u1ea3n l\u00fd b\u1ed9 nh\u1edb b\u1eb1ng c\u00e1ch gi\u1ea3m m\u1ee9c ti\u00eau th\u1ee5 b\u1ed9 nh\u1edb.<\/p>\n<p>Khi b\u1ea1n th\u00eam c\u00e1c thu\u1ed9c t\u00ednh v\u00e0 ph\u01b0\u01a1ng th\u1ee9c v\u00e0o prototype c\u1ee7a m\u1ed9t object, ch\u00fang s\u1ebd \u0111\u01b0\u1ee3c chia s\u1ebb gi\u1eefa t\u1ea5t c\u1ea3 c\u00e1c instance c\u1ee7a object \u0111\u00f3, thay v\u00ec b\u1ecb tr\u00f9ng l\u1eb7p trong m\u1ed7i instance. Do \u0111\u00f3, c\u00e1c instance ch\u1ec9 c\u1ea7n l\u01b0u tr\u1eef d\u1eef li\u1ec7u duy nh\u1ea5t c\u1ee7a ch\u00fang, d\u1eabn \u0111\u1ebfn m\u1ed9t thi\u1ebft k\u1ebf ti\u1ebft ki\u1ec7m b\u1ed9 nh\u1edb h\u01a1n.<\/p>\n<p>\u01afu \u0111i\u1ec3m n\u00e0y mang l\u1ea1i gi\u00e1 tr\u1ecb r\u00f5 r\u1ec7t khi b\u1ea1n ph\u1ea3i x\u1eed l\u00fd m\u1ed9t s\u1ed1 l\u01b0\u1ee3ng \u0111\u00e1ng k\u1ec3 c\u00e1c instance ho\u1eb7c x\u1eed l\u00fd c\u00e1c t\u00e1c v\u1ee5 s\u1eed d\u1ee5ng nhi\u1ec1u t\u00e0i nguy\u00ean. Do \u0111\u00f3, prototype s\u1ebd gi\u00fap b\u1ea1n c\u1ea3i thi\u1ec7n \u0111\u00e1ng k\u1ec3 hi\u1ec7u su\u1ea5t \u1ee9ng d\u1ee5ng v\u00e0 \u0111\u1ea3m b\u1ea3o tr\u1ea3i nghi\u1ec7m ng\u01b0\u1eddi d\u00f9ng m\u01b0\u1ee3t m\u00e0 h\u01a1n.<\/p>\n<h3><b>T\u1ed1i \u01b0u h\u00f3a hi\u1ec7u su\u1ea5t<\/b><\/h3>\n<p>Prototype \u0111\u00f3ng m\u1ed9t vai tr\u00f2 quan tr\u1ecdng trong vi\u1ec7c t\u1ed1i \u01b0u h\u00f3a hi\u1ec7u su\u1ea5t trong c\u00e1c \u1ee9ng d\u1ee5ng JavaScript.<\/p>\n<p>Khi b\u1ea1n x\u00e1c \u0111\u1ecbnh c\u00e1c ph\u01b0\u01a1ng th\u1ee9c v\u00e0 thu\u1ed9c t\u00ednh \u0111\u01b0\u1ee3c chia s\u1ebb trong prototype, JavaScript c\u00f3 th\u1ec3 truy c\u1eadp ch\u00fang m\u1ed9t c\u00e1ch hi\u1ec7u qu\u1ea3. Thay v\u00ec t\u00ecm ki\u1ebfm thu\u1ed9c t\u00ednh trong t\u1eebng instance ri\u00eang l\u1ebb, JavaScript \u0111i theo prototype chain \u0111\u1ec3 t\u00ecm ch\u1ee9c n\u0103ng \u0111\u01b0\u1ee3c chia s\u1ebb, d\u1eabn \u0111\u1ebfn th\u1eddi gian truy c\u1eadp nhanh h\u01a1n.<\/p>\n<p>Vi\u1ec7c t\u1ed1i \u01b0u h\u00f3a n\u00e0y ng\u00e0y c\u00e0ng tr\u1edf n\u00ean quan tr\u1ecdng trong c\u00e1c \u1ee9ng d\u1ee5ng quan tr\u1ecdng v\u1ec1 hi\u1ec7u su\u1ea5t, ch\u1eb3ng h\u1ea1n nh\u01b0 tr\u00f2 ch\u01a1i th\u1eddi gian th\u1ef1c ho\u1eb7c c\u00e1c \u1ee9ng d\u1ee5ng s\u1eed d\u1ee5ng nhi\u1ec1u d\u1eef li\u1ec7u.<\/p>\n<blockquote><p><em><a href=\"https:\/\/itviec.com\/it-jobs\/javascript\/ho-chi-minh-hcm\/\" target=\"_blank\" rel=\"noopener\"><strong>Vi\u1ec7c l\u00e0m JavaScript \u1edf TP. HCM<\/strong><\/a> tr\u00ean ITviec<\/em><\/p>\n<p><em><a href=\"https:\/\/itviec.com\/it-jobs\/javascript\/ha-noi\" target=\"_blank\" rel=\"noopener\"><strong>Vi\u1ec7c l\u00e0m JavaScript \u1edf H\u00e0 N\u1ed9i<\/strong><\/a> tr\u00ean ITviec<\/em><\/p><\/blockquote>\n<h2><span class=\"ez-toc-section\" id=\"Cach_tao_prototype\"><\/span><strong>C\u00e1ch t\u1ea1o prototype<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>C\u00f9ng nh\u1eafc l\u1ea1i object prototype l\u00e0 g\u00ec: Object prototype l\u00e0 m\u1ed9t thu\u1ed9c t\u00ednh \u1ea9n v\u00e0 c\u00f3 th\u1ec3 \u0111\u01b0\u1ee3c truy c\u1eadp b\u1eb1ng ph\u01b0\u01a1ng th\u1ee9c <code>Object.getPrototypeOf()<\/code> ho\u1eb7c thu\u1ed9c t\u00ednh <code>__proto__<\/code> .<\/p>\n<p>V\u1ec1 c\u01a1 b\u1ea3n, \u0111\u00e2y l\u00e0 m\u1ed9t ph\u01b0\u01a1ng ph\u00e1p chi ti\u1ebft \u0111\u1ec3 t\u1ea1o c\u00e1c instance m\u1edbi c\u1ee7a m\u1ed9t object v\u00e0 cho ph\u00e9p c\u00e1c object k\u1ebf th\u1eeba c\u00e1c thu\u1ed9c t\u00ednh v\u00e0 ph\u01b0\u01a1ng th\u1ee9c t\u1eeb prototype c\u1ee7a ch\u00fang.<\/p>\n<p>V\u00ed d\u1ee5 1: T\u1ea1o v\u00e0 truy c\u1eadp prototype<\/p>\n<pre>\/\/ T\u1ea1o m\u1ed9t object v\u1edbi 1 prototype\r\n\r\nconst personPrototype = {\r\n\r\n\u00a0\u00a0greet: function() {\r\n\r\n\u00a0\u00a0\u00a0\u00a0return `Hello, my name is ${this.name} and I'm ${this.age} years old.`;\r\n\r\n\u00a0\u00a0},\r\n\r\n};\r\n\r\nconst person1 = Object.create(personPrototype);\r\n\r\nperson1.name = \"John\";\r\n\r\nperson1.age = 30;\r\n\r\nconsole.log(person1.greet()); \/\/ \u0110\u1ea7u ra: \"Hello, my name is John and I'm 30 years old.\"\r\n\r\nconst person2 = Object.create(personPrototype);\r\n\r\nperson2.name = \"Alice\";\r\n\r\nperson2.age = 25;\r\n\r\nconsole.log(person2.greet()); \/\/ \u0110\u1ea7u ra: \"Hello, my name is Alice and I'm 25 years old.\"<\/pre>\n<p>V\u00ed d\u1ee5 2: Th\u00eam ph\u01b0\u01a1ng th\u1ee9c v\u00e0o prototype<\/p>\n<pre>function Person(name, age) {\r\n\r\n\u00a0\u00a0this.name = name;\r\n\r\n\u00a0\u00a0this.age = age;\r\n\r\n}\r\n\r\nPerson.prototype.greet = function() {\r\n\r\n\u00a0\u00a0return `Hello, my name is ${this.name} and I'm ${this.age} years old.`;\r\n\r\n};\r\n\r\nconst person1 = new Person(\"John\", 30);\r\n\r\nconsole.log(person1.greet()); \/\/ \u0110\u1ea7u ra: \"Hello, my name is John and I'm 30 years old.\"<\/pre>\n<p>Trong v\u00ed d\u1ee5 th\u1ee9 hai, ch\u00fang ta \u0111\u1ecbnh ngh\u0129a m\u1ed9t h\u00e0m x\u00e2y d\u1ef1ng (constructor function) Person v\u00e0 th\u00eam m\u1ed9t ph\u01b0\u01a1ng th\u1ee9c ch\u00e0o (greet method) v\u00e0o prototype c\u1ee7a ch\u00fang. Khi ch\u00fang ta t\u1ea1o m\u1ed9t instance m\u1edbi c\u1ee7a Person b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng t\u1eeb kh\u00f3a new, instance n\u00e0y s\u1ebd t\u1ef1 \u0111\u1ed9ng li\u00ean k\u1ebft v\u1edbi Person.prototype, cho ph\u00e9p truy c\u1eadp ph\u01b0\u01a1ng th\u1ee9c ch\u00e0o th\u00f4ng qua <a href=\"https:\/\/itviec.com\/blog\/prototype-chain-la-gi\" target=\"_blank\" rel=\"noopener\">prototype chain<\/a>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Cach_su_dung_va_thao_tac_voi_prototype\"><\/span><strong>C\u00e1ch s\u1eed d\u1ee5ng v\u00e0 thao t\u00e1c v\u1edbi prototype<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><b>1. S\u1eed d\u1ee5ng constructor v\u00e0 t\u1eeb kh\u00f3a new<\/b><\/h3>\n<p>Trong JavaScript, b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng c\u00e1c prototype k\u1ebft h\u1ee3p v\u1edbi c\u00e1c h\u00e0m constructor v\u00e0 t\u1eeb kh\u00f3a new \u0111\u1ec3 tri\u1ec3n khai l\u1eadp tr\u00ecnh h\u01b0\u1edbng \u0111\u1ed1i t\u01b0\u1ee3ng (<a href=\"https:\/\/itviec.com\/blog\/oop-la-gi\/\" target=\"_blank\" rel=\"noopener\">OOP<\/a>) v\u00e0 t\u1ea1o c\u00e1c object c\u00f3 ch\u1ee9c n\u0103ng d\u00f9ng chung.<\/p>\n<p>T\u1eeb kh\u00f3a new \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 kh\u1edfi t\u1ea1o c\u00e1c object t\u1eeb c\u00e1c h\u00e0m constructor v\u00e0 c\u00e1c prototype \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng \u0111\u1ec3 x\u00e1c \u0111\u1ecbnh c\u00e1c thu\u1ed9c t\u00ednh v\u00e0 ph\u01b0\u01a1ng th\u1ee9c d\u00f9ng chung cho c\u00e1c object \u0111\u00f3. H\u00e3y xem qua c\u00e1c b\u01b0\u1edbc \u0111\u1ec3 s\u1eed d\u1ee5ng prototype v\u1edbi h\u00e0m constructor v\u00e0 t\u1eeb kh\u00f3a new:<\/p>\n<ul>\n<li aria-level=\"1\"><strong>B\u01b0\u1edbc 1: X\u00e1c \u0111\u1ecbnh H\u00e0m Constructor<\/strong><\/li>\n<\/ul>\n<p>\u0110\u1ea7u ti\u00ean, b\u1ea1n c\u1ea7n \u0111\u1ecbnh ngh\u0129a m\u1ed9t h\u00e0m constructor. H\u00e0m n\u00e0y ho\u1ea1t \u0111\u1ed9ng nh\u01b0 m\u1ed9t k\u1ebf ho\u1ea1ch chi ti\u1ebft \u0111\u1ec3 t\u1ea1o c\u00e1c object c\u00f3 c\u00e1c thu\u1ed9c t\u00ednh v\u00e0 ph\u01b0\u01a1ng th\u1ee9c t\u01b0\u01a1ng t\u1ef1.<\/p>\n<p>B\u00ean trong h\u00e0m constructor, b\u1ea1n c\u00f3 th\u1ec3 thi\u1ebft l\u1eadp c\u00e1c thu\u1ed9c t\u00ednh object b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng t\u1eeb kh\u00f3a this.<\/p>\n<pre>function Person(name, age) {\r\n\r\n\u00a0\u00a0this.name = name;\r\n\r\n\u00a0\u00a0this.age = age;\r\n\r\n}<\/pre>\n<ul>\n<li aria-level=\"1\"><strong>B\u01b0\u1edbc 2: Th\u00eam c\u00e1c ph\u01b0\u01a1ng th\u1ee9c d\u00f9ng chung v\u00e0o prototype<\/strong><\/li>\n<\/ul>\n<p>Ti\u1ebfp theo, b\u1ea1n c\u00f3 th\u1ec3 m\u1edf r\u1ed9ng prototype c\u1ee7a h\u00e0m n\u00e0y \u0111\u1ec3 th\u00eam c\u00e1c ph\u01b0\u01a1ng th\u1ee9c d\u00f9ng chung (shared method) m\u00e0 t\u1ea5t c\u1ea3 c\u00e1c instance s\u1ebd k\u1ebf th\u1eeba. B\u1eb1ng c\u00e1ch s\u1eeda \u0111\u1ed5i prototype, b\u1ea1n \u0111\u1ea3m b\u1ea3o r\u1eb1ng m\u1ed7i object m\u1edbi \u0111\u01b0\u1ee3c t\u1ea1o t\u1eeb h\u00e0m constructor s\u1ebd chia s\u1ebb c\u00e1c ph\u01b0\u01a1ng th\u1ee9c n\u00e0y, tr\u00e1nh tr\u00f9ng l\u1eb7p kh\u00f4ng c\u1ea7n thi\u1ebft.<\/p>\n<pre>Person.prototype.sayHello = function() {\r\n\r\n\u00a0\u00a0return `Hello, my name is ${this.name}, and I am ${this.age} years old.`;\r\n\r\n};<\/pre>\n<ul>\n<li aria-level=\"1\"><strong>B\u01b0\u1edbc 3: T\u1ea1o Object Instances v\u1edbi t\u1eeb kh\u00f3a new<\/strong><\/li>\n<\/ul>\n<p>B\u00e2y gi\u1edd, b\u1ea1n c\u00f3 th\u1ec3 t\u1ea1o c\u00e1c instance c\u1ee7a object b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng t\u1eeb kh\u00f3a new v\u00e0 h\u00e0m constructor. Khi s\u1eed d\u1ee5ng new, m\u1ed9t object tr\u1ed1ng m\u1edbi \u0111\u01b0\u1ee3c t\u1ea1o v\u00e0 g\u00e1n cho this, cho ph\u00e9p h\u00e0m constructor thi\u1ebft l\u1eadp c\u00e1c thu\u1ed9c t\u00ednh v\u00e0 ph\u01b0\u01a1ng th\u1ee9c tr\u00ean object m\u1edbi \u0111\u01b0\u1ee3c t\u1ea1o.<\/p>\n<pre>const person1 = new Person(\"John\", 30);\r\n\r\nconst person2 = new Person(\"Alice\", 25);\r\n\r\n\r\nconsole.log(person1.sayHello()); \/\/ \u0110\u1ea7u ra: \"Hello, my name is John, and I am 30 years old.\"\r\n\r\nconsole.log(person2.sayHello()); \/\/ \u0110\u1ea7u ra: \"Hello, my name is Alice, and I am 25 years old.\"<\/pre>\n<ul>\n<li aria-level=\"1\"><strong>B\u01b0\u1edbc 4: K\u1ebf th\u1eeba object th\u00f4ng qua prototype<\/strong><\/li>\n<\/ul>\n<p>Nh\u1edd c\u00e1c prototype, c\u1ea3 person1 v\u00e0 person2 \u0111\u1ec1u k\u1ebf th\u1eeba ph\u01b0\u01a1ng th\u1ee9c sayHello t\u1eeb Person.prototype. C\u00e1c instance n\u00e0y chia s\u1ebb c\u00f9ng m\u1ed9t ph\u01b0\u01a1ng th\u1ee9c, nh\u01b0ng ch\u00fang c\u00f3 th\u1ec3 ch\u1ee9a d\u1eef li\u1ec7u duy nh\u1ea5t (v\u00ed d\u1ee5: t\u00ean v\u00e0 tu\u1ed5i kh\u00e1c nhau) m\u00e0 kh\u00f4ng c\u1ea7n sao ch\u00e9p ch\u00ednh ph\u01b0\u01a1ng th\u1ee9c \u0111\u00f3.<\/p>\n<p>B\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng c\u00e1c prototype v\u1edbi h\u00e0m constructor v\u00e0 t\u1eeb kh\u00f3a new, b\u1ea1n c\u00f3 th\u1ec3 t\u1ea1o v\u00e0 qu\u1ea3n l\u00fd hi\u1ec7u qu\u1ea3 c\u00e1c instance c\u1ee7a object v\u1edbi c\u00e1c thu\u1ed9c t\u00ednh v\u00e0 ph\u01b0\u01a1ng th\u1ee9c d\u00f9ng chung, th\u00fac \u0111\u1ea9y kh\u1ea3 n\u0103ng t\u00e1i s\u1eed d\u1ee5ng code v\u00e0 hi\u1ec7u su\u1ea5t b\u1ed9 nh\u1edb trong c\u00e1c \u1ee9ng d\u1ee5ng JavaScript c\u1ee7a b\u1ea1n.<\/p>\n<h3><b>2. Thay \u0111\u1ed5i prototype<\/b><\/h3>\n<p>Trong JavaScript, b\u1ea1n c\u00f3 th\u1ec3 thay \u0111\u1ed5i prototype c\u1ee7a m\u1ed9t h\u00e0m constructor object \u0111\u1ec3 s\u1eeda \u0111\u1ed5i t\u1ea5t c\u1ea3 c\u00e1c instance k\u1ebf th\u1eeba. Thay \u0111\u1ed5i prototype cho ph\u00e9p b\u1ea1n th\u00eam c\u00e1c thu\u1ed9c t\u00ednh ho\u1eb7c ph\u01b0\u01a1ng th\u1ee9c m\u1edbi ho\u1eb7c s\u1eeda \u0111\u1ed5i c\u00e1c thu\u1ed9c t\u00ednh ho\u1eb7c ph\u01b0\u01a1ng th\u1ee9c hi\u1ec7n c\u00f3 m\u1ed9t c\u00e1ch linh ho\u1ea1t trong th\u1eddi gian ch\u1ea1y theo c\u00e1c c\u00e1ch sau:<\/p>\n<ul>\n<li aria-level=\"1\"><strong>S\u1eeda \u0111\u1ed5i prototype hi\u1ec7n c\u00f3:<\/strong><\/li>\n<\/ul>\n<p>N\u1ebfu b\u1ea1n mu\u1ed1n th\u00eam m\u1ed9t ph\u01b0\u01a1ng th\u1ee9c ho\u1eb7c thu\u1ed9c t\u00ednh m\u1edbi v\u00e0o prototype hi\u1ec7n c\u00f3, b\u1ea1n c\u00f3 th\u1ec3 th\u1ef1c hi\u1ec7n tr\u1ef1c ti\u1ebfp b\u1eb1ng c\u00e1ch g\u00e1n cho prototype.<\/p>\n<pre>function Person(name, age) {\r\n\r\n\u00a0\u00a0this.name = name;\r\n\r\n\u00a0\u00a0this.age = age;\r\n\r\n}\r\n\r\n\r\nPerson.prototype.sayHello = function() {\r\n\r\n\u00a0\u00a0return `Hello, my name is ${this.name}, and I am ${this.age} years old.`;\r\n\r\n};\r\n\r\n\r\nconst person1 = new Person(\"John\", 30);\r\n\r\nconsole.log(person1.sayHello()); \/\/ \u0110\u1ea7u ra: \"Hello, my name is John, and I am 30 years old.\"\r\n\r\n\r\n\/\/ Ch\u1ec9nh s\u1eeda prototype \u0111\u00e3 t\u1ed3n t\u1ea1i\r\n\r\nPerson.prototype.sayGoodbye = function() {\r\n\r\n\u00a0\u00a0return `Goodbye from ${this.name}!`;\r\n\r\n};\r\n\r\n\r\nconsole.log(person1.sayGoodbye()); \/\/ \u0110\u1ea7u ra: \"Goodbye from John!\"<\/pre>\n<ul>\n<li aria-level=\"1\"><strong>Thay th\u1ebf to\u00e0n b\u1ed9 prototype:<\/strong><\/li>\n<\/ul>\n<p>N\u1ebfu b\u1ea1n mu\u1ed1n thay th\u1ebf to\u00e0n b\u1ed9 prototype b\u1eb1ng m\u1ed9t object m\u1edbi, b\u1ea1n c\u00f3 th\u1ec3 l\u00e0m nh\u01b0 v\u1eady b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng ph\u01b0\u01a1ng th\u1ee9c Object.create(). Ph\u01b0\u01a1ng th\u1ee9c n\u00e0y cho ph\u00e9p b\u1ea1n t\u1ea1o m\u1ed9t object m\u1edbi v\u00e0 \u0111\u1eb7t object \u0111\u00f3 l\u00e0m prototype c\u1ee7a h\u00e0m constructor.<\/p>\n<pre>function Person(name, age) {\r\n\r\n\u00a0\u00a0this.name = name;\r\n\r\n\u00a0\u00a0this.age = age;\r\n\r\n}\r\n\r\n\r\nPerson.prototype.sayHello = function() {\r\n\r\n\u00a0\u00a0return `Hello, my name is ${this.name}, and I am ${this.age} years old.`;\r\n\r\n};\r\n\r\n\r\nconst person1 = new Person(\"John\", 30);\r\n\r\nconsole.log(person1.sayHello()); \/\/ \u0110\u1ea7u ra: \"Hello, my name is John, and I am 30 years old.\"\r\n\r\n\r\n\/\/ Thay th\u1ebf to\u00e0n b\u1ed9 prototype\r\n\r\nconst newPrototype = {\r\n\r\n\u00a0\u00a0sayGoodbye() {\r\n\r\n\u00a0\u00a0\u00a0\u00a0return `Goodbye from ${this.name}!`;\r\n\r\n\u00a0\u00a0},\r\n\r\n};\r\n\r\n\r\nPerson.prototype = newPrototype;\r\n\r\n\r\nconsole.log(person1.sayGoodbye()); \/\/ \u0110\u1ea7u ra: \"Goodbye from John!\"<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Moi_quan_he_giua_tinh_ke_thua_va_prototype_la_gi\"><\/span><strong>M\u1ed1i quan h\u1ec7 gi\u1eefa t\u00ednh k\u1ebf th\u1eeba <\/strong><strong> v\u00e0 prototype l\u00e0 g\u00ec?<br \/>\n<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Khi n\u00f3i \u0111\u1ebfn t\u00ednh k\u1ebf th\u1eeba, JavaScript ch\u1ec9 c\u00f3 m\u1ed9t c\u1ea5u tr\u00fac: object. M\u1ed7i object c\u00f3 m\u1ed9t thu\u1ed9c t\u00ednh ri\u00eang ch\u1ee9a li\u00ean k\u1ebft \u0111\u1ebfn m\u1ed9t object kh\u00e1c \u0111\u01b0\u1ee3c g\u1ecdi l\u00e0 prototype. Prototype object \u0111\u00f3 l\u1ea1i c\u00f3 m\u1ed9t prototype ri\u00eang, v.v. cho \u0111\u1ebfn khi c\u00f3 m\u1ed9t object m\u00e0 prototype c\u1ee7a n\u00f3 l\u00e0 <code>null<\/code>.<\/p>\n<p>Theo \u0111\u1ecbnh ngh\u0129a, <code>null<\/code> kh\u00f4ng c\u00f3 prototype v\u00e0 \u0111\u00f3ng vai tr\u00f2 l\u00e0 li\u00ean k\u1ebft cu\u1ed1i c\u00f9ng trong prototype chain n\u00e0y. C\u00f3 th\u1ec3 thay \u0111\u1ed5i b\u1ea5t k\u1ef3 th\u00e0nh vi\u00ean n\u00e0o c\u1ee7a prototype chain ho\u1eb7c th\u1eadm ch\u00ed ho\u00e1n \u0111\u1ed5i prototype trong th\u1eddi gian ch\u1ea1y, do \u0111\u00f3, c\u00e1c kh\u00e1i ni\u1ec7m nh\u01b0 static dispatching kh\u00f4ng t\u1ed3n t\u1ea1i trong JavaScript.<\/p>\n<p>M\u1eb7c d\u00f9 vi\u1ec7c n\u00e0y th\u01b0\u1eddng \u0111\u01b0\u1ee3c coi l\u00e0 m\u1ed9t trong nh\u1eefng \u0111i\u1ec3m y\u1ebfu c\u1ee7a JavaScript, nh\u01b0ng tr\u00ean th\u1ef1c t\u1ebf, b\u1ea3n th\u00e2n m\u00f4 h\u00ecnh k\u1ebf th\u1eeba theo prototype l\u1ea1i m\u1ea1nh h\u01a1n nh\u1eefng m\u00f4 h\u00ecnh k\u1ebf th\u1eeba c\u1ed5 \u0111i\u1ec3n th\u01b0\u1eddng th\u1ea5y \u1edf nh\u1eefng ng\u00f4n ng\u1eef kh\u00e1c. V\u00ed d\u1ee5, vi\u1ec7c x\u00e2y d\u1ef1ng m\u1ed9t m\u00f4 h\u00ecnh c\u1ed5 \u0111i\u1ec3n tr\u00ean m\u1ed9t m\u00f4 h\u00ecnh prototype l\u00e0 kh\u00e1 \u0111\u01a1n gi\u1ea3n.<\/p>\n<p>M\u1eb7c d\u00f9 c\u00e1c class hi\u1ec7n \u0111\u00e3 \u0111\u01b0\u1ee3c \u00e1p d\u1ee5ng r\u1ed9ng r\u00e3i v\u00e0 \u0111\u00e3 tr\u1edf th\u00e0nh m\u1ed9t m\u00f4 h\u00ecnh (model) m\u1edbi trong JavaScript, nh\u01b0ng c\u00e1c class kh\u00f4ng mang l\u1ea1i m\u1ed9t pattern k\u1ebf th\u1eeba m\u1edbi. Trong khi c\u00e1c class l\u1ea1i tr\u1eebu t\u01b0\u1ee3ng h\u00f3a h\u1ea7u h\u1ebft c\u01a1 ch\u1ebf prototype, th\u00ec vi\u1ec7c hi\u1ec3u c\u00e1ch prototype ho\u1ea1t \u0111\u1ed9ng nh\u01b0 th\u1ebf n\u00e0o v\u1eabn c\u00f3 nh\u1eefng l\u1ee3i \u00edch nh\u1ea5t \u0111\u1ecbnh trong vi\u1ec7c tri\u1ec3n khai.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Nhung_cau_hoi_thuong_gap_ve_prototype_trong_Javascript\"><\/span><strong>Nh\u1eefng c\u00e2u h\u1ecfi th\u01b0\u1eddng g\u1eb7p v\u1ec1 prototype trong Javascript<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><b>1. Prototype cho ph\u00e9p k\u1ebf th\u1eeba object nh\u01b0 th\u1ebf n\u00e0o?<\/b><\/h3>\n<p>Prototype cho ph\u00e9p k\u1ebf th\u1eeba object b\u1eb1ng c\u00e1ch cho ph\u00e9p c\u00e1c object chia s\u1ebb c\u00e1c thu\u1ed9c t\u00ednh v\u00e0 ph\u01b0\u01a1ng th\u1ee9c v\u1edbi c\u00e1c prototype c\u1ee7a ch\u00fang.<\/p>\n<p>Khi m\u1ed9t thu\u1ed9c t\u00ednh ho\u1eb7c ph\u01b0\u01a1ng th\u1ee9c \u0111\u01b0\u1ee3c truy c\u1eadp tr\u00ean m\u1ed9t object, tr\u01b0\u1edbc ti\u00ean, JavaScript s\u1ebd t\u00ecm trong ch\u00ednh object \u0111\u00f3. N\u1ebfu kh\u00f4ng t\u00ecm th\u1ea5y, h\u1ec7 th\u1ed1ng ti\u1ebfp t\u1ee5c t\u00ecm ki\u1ebfm trong prototype c\u1ee7a object, v\u00e0 ti\u1ebfp t\u1ee5c nh\u01b0 v\u1eady trong prototype chain.<\/p>\n<h3><b>2. L\u00e0m th\u1ebf n\u00e0o \u0111\u1ec3 b\u1ea1n t\u1ea1o m\u1ed9t prototype trong JavaScript?<\/b><\/h3>\n<p>B\u1ea1n c\u00f3 th\u1ec3 t\u1ea1o m\u1ed9t prototype b\u1eb1ng c\u00e1ch x\u00e1c \u0111\u1ecbnh m\u1ed9t object ho\u1eb7c h\u00e0m constructor v\u00e0 th\u00eam c\u00e1c thu\u1ed9c t\u00ednh v\u00e0 ph\u01b0\u01a1ng th\u1ee9c v\u00e0o prototype c\u1ee7a ch\u00fang.<\/p>\n<p>\u0110\u1ed1i v\u1edbi c\u00e1c h\u00e0m constructor, b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng thu\u1ed9c t\u00ednh prototype \u0111\u1ec3 x\u00e1c \u0111\u1ecbnh c\u00e1c ch\u1ee9c n\u0103ng \u0111\u01b0\u1ee3c chia s\u1ebb.<\/p>\n<h3><b>3. Prototype c\u1ea3i thi\u1ec7n kh\u1ea3 n\u0103ng t\u00e1i s\u1eed d\u1ee5ng code nh\u01b0 th\u1ebf n\u00e0o?<\/b><\/h3>\n<p>B\u1eb1ng c\u00e1ch x\u00e1c \u0111\u1ecbnh c\u00e1c ch\u1ee9c n\u0103ng \u0111\u01b0\u1ee3c chia s\u1ebb trong prototype, b\u1ea1n c\u00f3 th\u1ec3 t\u1ea1o m\u1ed9t k\u1ebf ho\u1ea1ch chi ti\u1ebft cho nhi\u1ec1u instance \u0111\u1ec3 k\u1ebf th\u1eeba t\u1eeb \u0111\u00f3. \u0110i\u1ec1u n\u00e0y l\u00e0m gi\u1ea3m s\u1ef1 d\u01b0 th\u1eeba v\u00e0 th\u00fac \u0111\u1ea9y kh\u1ea3 n\u0103ng t\u00e1i s\u1eed d\u1ee5ng code, l\u00e0m cho codebase c\u00f3 t\u1ed5 ch\u1ee9c v\u00e0 d\u1ec5 b\u1ea3o tr\u00ec h\u01a1n.<\/p>\n<h3><b>4. M\u1ed1i quan h\u1ec7 gi\u1eefa h\u00e0m constructor v\u00e0 prototype l\u00e0 g\u00ec?<\/b><\/h3>\n<p>Trong JavaScript, m\u1ecdi h\u00e0m constructor \u0111\u1ec1u c\u00f3 thu\u1ed9c t\u00ednh prototype tr\u1ecf \u0111\u1ebfn object prototype. Khi b\u1ea1n t\u1ea1o m\u1ed9t object b\u1eb1ng h\u00e0m constructor v\u1edbi t\u1eeb kh\u00f3a new, thu\u1ed9c t\u00ednh <code>__proto__<\/code> c\u1ee7a object s\u1ebd li\u00ean k\u1ebft v\u1edbi prototype c\u1ee7a h\u00e0m constructor.<\/p>\n<h3><b>5. B\u1ea1n c\u00f3 th\u1ec3 thay \u0111\u1ed5i prototype c\u1ee7a m\u1ed9t object sau khi t\u1ea1o kh\u00f4ng?<\/b><\/h3>\n<p>C\u00f3, b\u1ea1n c\u00f3 th\u1ec3 s\u1eeda \u0111\u1ed5i prototype c\u1ee7a m\u1ed9t object trong th\u1eddi gian ch\u1ea1y. Nh\u1eefng thay \u0111\u1ed5i \u0111\u01b0\u1ee3c th\u1ef1c hi\u1ec7n \u0111\u1ed1i v\u1edbi prototype s\u1ebd \u0111\u01b0\u1ee3c ph\u1ea3n \u00e1nh trong t\u1ea5t c\u1ea3 c\u00e1c instance k\u1ebf th\u1eeba t\u1eeb prototype \u0111\u00f3.<\/p>\n<h3><b>6. Prototype c\u1ea3i thi\u1ec7n hi\u1ec7u su\u1ea5t nh\u01b0 th\u1ebf n\u00e0o?<\/b><\/h3>\n<p>Prototype c\u1ea3i thi\u1ec7n hi\u1ec7u su\u1ea5t b\u1eb1ng c\u00e1ch cho ph\u00e9p truy c\u1eadp c\u00e1c ph\u01b0\u01a1ng th\u1ee9c d\u00f9ng chung th\u00f4ng qua prototype chain. \u0110i\u1ec1u n\u00e0y l\u00e0m gi\u1ea3m th\u1eddi gian tra c\u1ee9u thu\u1ed9c t\u00ednh v\u00e0 d\u1eabn \u0111\u1ebfn t\u1ed1c \u0111\u1ed9 th\u1ef1c thi code t\u1ed1t h\u01a1n.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Tong_ket\"><\/span><b>T\u1ed5ng k\u1ebft<br \/>\n<\/b><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Prototype l\u00e0 m\u1ed9t kh\u00e1i ni\u1ec7m quan tr\u1ecdng trong JavaScript, cho ph\u00e9p ch\u00fang ta t\u1ea1o c\u00e1c m\u1eabu (templates) v\u00e0 chia s\u1ebb c\u00e1c thu\u1ed9c t\u00ednh v\u00e0 ph\u01b0\u01a1ng th\u1ee9c chung gi\u1eefa c\u00e1c object. Hi\u1ec3u prototype l\u00e0 g\u00ec v\u00e0 \u00e1p d\u1ee5ng ch\u00fang m\u1ed9t c\u00e1ch ch\u00ednh x\u00e1c gi\u00fap t\u1ed1i \u01b0u h\u00f3a vi\u1ec7c s\u1eed d\u1ee5ng b\u1ed9 nh\u1edb, t\u00e1i s\u1eed d\u1ee5ng code v\u00e0 x\u00e2y d\u1ef1ng c\u00e1c \u1ee9ng d\u1ee5ng JavaScript linh ho\u1ea1t v\u00e0 hi\u1ec7u qu\u1ea3.<\/p>\n<p>&nbsp;<\/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>\u0110\u1ed1i v\u1edbi m\u1ed9t ng\u00f4n ng\u1eef prototype-based nh\u01b0 JavaScript, prototype l\u00e0 m\u1ed9t kh\u00e1i ni\u1ec7m c\u1ed1t l\u00f5i m\u00e0 b\u1ea5t c\u1ee9 l\u1eadp tr\u00ecnh vi\u00ean JavaScript n\u00e0o c\u0169ng c\u1ea7n bi\u1ebft. H\u00e3y c\u00f9ng \u0111i qua nh\u1eefng ki\u1ebfn th\u1ee9c t\u1eeb c\u01a1 b\u1ea3n nh\u01b0 prototype l\u00e0 g\u00ec cho \u0111\u1ebfn c\u1ea7n thi\u1ebft nh\u1ea5t v\u1ec1 c\u00e1ch s\u1eed d\u1ee5ng prototype v\u00e0 \u00e1p d\u1ee5ng ch\u00fang hi\u1ec7u [&hellip;]<\/p>\n","protected":false},"author":95,"featured_media":39743,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_gspb_post_css":"","footnotes":""},"categories":[109,10106],"tags":[],"class_list":["post-39546","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-chuyen-mon-it","category-tai-lieu-js"],"blocksy_meta":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.8 (Yoast SEO v27.9) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Prototype l\u00e0 g\u00ec: \u0110\u1ecbnh ngh\u0129a, Ph\u00e2n lo\u1ea1i v\u00e0 C\u00e1ch s\u1eed d\u1ee5ng - ITviec<\/title>\n<meta name=\"description\" content=\"B\u00e0i vi\u1ebft s\u1ebd \u0111i qua nh\u1eefng ki\u1ebfn th\u1ee9c t\u1eeb c\u01a1 b\u1ea3n \u0111\u1ebfn c\u1ea7n thi\u1ebft nh\u1ea5t v\u1ec1 prototype l\u00e0 g\u00ec - m\u1ed9t kh\u00e1i ni\u1ec7m quan tr\u1ecdng trong JavaScript.\" \/>\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-trong-javascript-la-gi\/\" \/>\n<meta property=\"og:locale\" content=\"vi_VN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Prototype trong Javascript: \u0110\u1ecbnh ngh\u0129a, Ph\u00e2n lo\u1ea1i v\u00e0 C\u00e1ch s\u1eed d\u1ee5ng\" \/>\n<meta property=\"og:description\" content=\"\u0110\u1ed1i v\u1edbi m\u1ed9t ng\u00f4n ng\u1eef prototype-based nh\u01b0 JavaScript, prototype l\u00e0 m\u1ed9t kh\u00e1i ni\u1ec7m c\u1ed1t l\u00f5i m\u00e0 b\u1ea5t c\u1ee9 l\u1eadp tr\u00ecnh vi\u00ean JavaScript n\u00e0o c\u0169ng c\u1ea7n bi\u1ebft. H\u00e3y c\u00f9ng \u0111i\" \/>\n<meta property=\"og:url\" content=\"https:\/\/itviec.com\/blog\/prototype-trong-javascript-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-08-17T09:15:43+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2023\/08\/prototype-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=\"13 ph\u00fat\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Prototype l\u00e0 g\u00ec: \u0110\u1ecbnh ngh\u0129a, Ph\u00e2n lo\u1ea1i v\u00e0 C\u00e1ch s\u1eed d\u1ee5ng - ITviec","description":"B\u00e0i vi\u1ebft s\u1ebd \u0111i qua nh\u1eefng ki\u1ebfn th\u1ee9c t\u1eeb c\u01a1 b\u1ea3n \u0111\u1ebfn c\u1ea7n thi\u1ebft nh\u1ea5t v\u1ec1 prototype l\u00e0 g\u00ec - m\u1ed9t kh\u00e1i ni\u1ec7m quan tr\u1ecdng trong JavaScript.","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-trong-javascript-la-gi\/","og_locale":"vi_VN","og_type":"article","og_title":"Prototype trong Javascript: \u0110\u1ecbnh ngh\u0129a, Ph\u00e2n lo\u1ea1i v\u00e0 C\u00e1ch s\u1eed d\u1ee5ng","og_description":"\u0110\u1ed1i v\u1edbi m\u1ed9t ng\u00f4n ng\u1eef prototype-based nh\u01b0 JavaScript, prototype l\u00e0 m\u1ed9t kh\u00e1i ni\u1ec7m c\u1ed1t l\u00f5i m\u00e0 b\u1ea5t c\u1ee9 l\u1eadp tr\u00ecnh vi\u00ean JavaScript n\u00e0o c\u0169ng c\u1ea7n bi\u1ebft. H\u00e3y c\u00f9ng \u0111i","og_url":"https:\/\/itviec.com\/blog\/prototype-trong-javascript-la-gi\/","og_site_name":"ITviec Blog","article_publisher":"https:\/\/www.facebook.com\/ITviec","article_published_time":"2023-08-17T09:15:43+00:00","og_image":[{"width":950,"height":500,"url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2023\/08\/prototype-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":"13 ph\u00fat"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/itviec.com\/blog\/prototype-trong-javascript-la-gi\/#article","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/prototype-trong-javascript-la-gi\/"},"author":{"name":"Tuong Uyen","@id":"https:\/\/itviec.com\/blog\/#\/schema\/person\/e97d0e359f8840eaea7dc3a96006a8d4"},"headline":"Prototype trong Javascript: \u0110\u1ecbnh ngh\u0129a, Ph\u00e2n lo\u1ea1i v\u00e0 C\u00e1ch s\u1eed d\u1ee5ng","datePublished":"2023-08-17T09:15:43+00:00","mainEntityOfPage":{"@id":"https:\/\/itviec.com\/blog\/prototype-trong-javascript-la-gi\/"},"wordCount":4136,"publisher":{"@id":"https:\/\/itviec.com\/blog\/#organization"},"image":{"@id":"https:\/\/itviec.com\/blog\/prototype-trong-javascript-la-gi\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2023\/08\/prototype-la-gi-blog-vippro.jpg","articleSection":["Chuy\u00ean m\u00f4n IT","T\u00e0i li\u1ec7u JS"],"inLanguage":"vi"},{"@type":"WebPage","@id":"https:\/\/itviec.com\/blog\/prototype-trong-javascript-la-gi\/","url":"https:\/\/itviec.com\/blog\/prototype-trong-javascript-la-gi\/","name":"Prototype l\u00e0 g\u00ec: \u0110\u1ecbnh ngh\u0129a, Ph\u00e2n lo\u1ea1i v\u00e0 C\u00e1ch s\u1eed d\u1ee5ng - ITviec","isPartOf":{"@id":"https:\/\/itviec.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/itviec.com\/blog\/prototype-trong-javascript-la-gi\/#primaryimage"},"image":{"@id":"https:\/\/itviec.com\/blog\/prototype-trong-javascript-la-gi\/#primaryimage"},"thumbnailUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2023\/08\/prototype-la-gi-blog-vippro.jpg","datePublished":"2023-08-17T09:15:43+00:00","description":"B\u00e0i vi\u1ebft s\u1ebd \u0111i qua nh\u1eefng ki\u1ebfn th\u1ee9c t\u1eeb c\u01a1 b\u1ea3n \u0111\u1ebfn c\u1ea7n thi\u1ebft nh\u1ea5t v\u1ec1 prototype l\u00e0 g\u00ec - m\u1ed9t kh\u00e1i ni\u1ec7m quan tr\u1ecdng trong JavaScript.","breadcrumb":{"@id":"https:\/\/itviec.com\/blog\/prototype-trong-javascript-la-gi\/#breadcrumb"},"inLanguage":"vi","potentialAction":[{"@type":"ReadAction","target":["https:\/\/itviec.com\/blog\/prototype-trong-javascript-la-gi\/"]}]},{"@type":"ImageObject","inLanguage":"vi","@id":"https:\/\/itviec.com\/blog\/prototype-trong-javascript-la-gi\/#primaryimage","url":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2023\/08\/prototype-la-gi-blog-vippro.jpg","contentUrl":"https:\/\/itviec.com\/blog\/wp-content\/uploads\/2023\/08\/prototype-la-gi-blog-vippro.jpg","width":950,"height":500,"caption":"prototype javascript - prototype l\u00e0 g\u00ec - itviec blog"},{"@type":"BreadcrumbList","@id":"https:\/\/itviec.com\/blog\/prototype-trong-javascript-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 trong Javascript: \u0110\u1ecbnh ngh\u0129a, Ph\u00e2n lo\u1ea1i v\u00e0 C\u00e1ch s\u1eed d\u1ee5ng"}]},{"@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\/39546","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=39546"}],"version-history":[{"count":0,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/posts\/39546\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media\/39743"}],"wp:attachment":[{"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/media?parent=39546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/categories?post=39546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itviec.com\/blog\/wp-json\/wp\/v2\/tags?post=39546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}