
{"id":971,"date":"2012-03-09T02:15:54","date_gmt":"2012-03-09T02:15:54","guid":{"rendered":"http:\/\/congnghepc.com\/2012\/03\/09\/cac-buoc-co-ban-tao-plugin-bang-jquery\/"},"modified":"2017-03-17T14:38:21","modified_gmt":"2017-03-17T14:38:21","slug":"cac-buoc-co-ban-tao-plugin-bang-jquery","status":"publish","type":"post","link":"https:\/\/tapchicntt.com\/cac-buoc-co-ban-tao-plugin-bang-jquery\/","title":{"rendered":"C\u00e1c b\u01b0\u1edbc c\u01a1 b\u1ea3n \u0111\u1ec3 t\u1ea1o plugin b\u1eb1ng jQuery"},"content":{"rendered":"<h2>\n\tCh&uacute;ng ta g\u1ea7n nh\u01b0 kh&ocirc;ng th\u1ec3 hi\u1ec3u \u0111\u01b0\u1ee3c jQuery n\u1ebfu kh&ocirc;ng n\u1eafm r&otilde; CSS Selectors:<\/h2>\n<p>\n\tTrong jQuery th&igrave; ph\u1ea7n m&atilde; <strong>CSS selector<\/strong> \u0111\u01b0\u1ee3c b\u1eaft \u0111\u1ea7u b\u1edfi \u0111\u1ed1i t\u01b0\u1ee3ng <strong>jQuery $<\/strong> ch&iacute;nh, \u0111&acirc;y ch\u1ec9 l&agrave; t&ecirc;n c\u1ee7a <strong>function<\/strong>, v&agrave; n&oacute; c&oacute; \u0111i k&egrave;m v\u1edbi tham s\u1ed1 &ndash; ch&iacute;nh l&agrave; <strong>CSS Selector<\/strong> c\u1ee7a ch&uacute;ng ta. V&agrave; v\u1ec1 b\u1ea3n ch\u1ea5t th&igrave; <strong>Selector <\/strong>ho\u1ea1t \u0111\u1ed9ng t\u01b0\u01a1ng t\u1ef1 nh\u01b0 c&aacute;c <strong>CSS selector<\/strong> th\u01b0\u1eddng g\u1eb7p trong c&aacute;c file <strong>style.css<\/strong>. Nhi\u1ec1u ng\u01b0\u1eddi ch\u1ec9 bi\u1ebft \u0111\u1ebfn <strong>ID selector (#)<\/strong> v&agrave; <strong>class selector (.)<\/strong> nh\u01b0ng ch&uacute;ng ch\u1ec9 l&agrave; nh\u1eefng \u0111\u1ed1i t\u01b0\u1ee3ng nh\u1ea5t \u0111\u1ecbnh thu\u1ed9c v\u1ec1 b\u1ed9 <strong>Selectors<\/strong>:<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"CSS Selectors\" src=\"http:\/\/farm4.staticflickr.com\/3707\/33108386100_0c62a1d607_o.png\" style=\"border-width: 0px; border-style: solid; width: 600px; height: 324px;\" \/><\/p>\n<p style=\"text-align: justify;\">\n\tN\u1ebfu b\u1ea1n c&oacute; th\u1ec3 hi\u1ec3u \u0111\u01b0\u1ee3c c&aacute;c chi ti\u1ebft trong bi\u1ec3u \u0111\u1ed3 tr&ecirc;n th&igrave; nh\u1eefng ph\u1ea7n c&ograve;n l\u1ea1i c\u1ee7a <strong>jQuery <\/strong>c\u0169ng kh&ocirc;ng c&oacute; g&igrave; kh&oacute; kh\u0103n \u0111\u1ec3 n\u1eafm b\u1eaft. V&agrave; m\u1ed9t khi \u0111&atilde; bi\u1ebft \u0111\u01b0\u1ee3c nh\u1eefng nguy&ecirc;n t\u1eafc c\u01a1 b\u1ea3n nh\u1ea5t c\u1ee7a quy tr&igrave;nh vi\u1ebft plugin jQuery th&igrave; h\u1ea7u nh\u01b0 kh&ocirc;ng c&oacute; ai mu\u1ed1n quay tr\u1edf v\u1ec1 v\u1edbi <strong>Javascript<\/strong>. D\u01b0\u1edbi \u0111&acirc;y l&agrave; m\u1ed9t s\u1ed1 c&acirc;u h\u1ecfi th\u01b0\u1eddng g\u1eb7p khi ch&uacute;ng ta b\u1eaft tay v&agrave;o qu&aacute; tr&igrave;nh vi\u1ebft plugin jQuery:<\/p>\n<ul>\n<li>\n\t\t<strong>CSS Selectors<\/strong> l&agrave; g&igrave;?<\/li>\n<li>\n\t\tS\u1ef1 kh&aacute;c bi\u1ec7t khi s\u1eed d\u1ee5ng <strong>$.myfunction<\/strong> v&agrave; <strong>$.fn.myfunction<\/strong> l&agrave; g&igrave;?<\/li>\n<li>\n\t\tK&yacute; hi\u1ec7u <strong>$<\/strong> ngh\u0129a l&agrave; g&igrave;?<\/li>\n<li>\n\t\tCh\u1ee9c n\u0103ng ch&iacute;nh v&agrave; c&aacute;ch s\u1eed d\u1ee5ng c\u1ee7a function <strong>jQuery.extend<\/strong>?<\/li>\n<li>\n\t\tL&agrave;m th\u1ebf n&agrave;o \u0111\u1ec3 kh\u1edfi t\u1ea1o plugin <strong>jQuery <\/strong>v&agrave; chuy\u1ec3n t\u1edbi tham s\u1ed1 ch\u1ee9c n\u0103ng?<\/li>\n<li>\n\t\tL&agrave;m th\u1ebf n&agrave;o \u0111\u1ec3 truy\u1ec1n gi&aacute; tr\u1ecb <strong>default <\/strong>v&agrave; kh\u1edfi t\u1ea1o tham s\u1ed1 <strong>override<\/strong>?<\/li>\n<\/ul>\n<h2>\n\t\u0110\u1ed1i t\u01b0\u1ee3ng trong JavaScript:<\/h2>\n<p>\n\tNh\u01b0 t\u1ea5t c\u1ea3 ch&uacute;ng ta \u0111&atilde; bi\u1ebft r\u1eb1ng, JavaScript l&agrave; 1 ng&ocirc;n ng\u1eef l\u1eadp tr&igrave;nh h\u01b0\u1edbng \u0111\u1ed1i t\u01b0\u1ee3ng, v&agrave; n&oacute; kh&ocirc;ng ph&ugrave; h\u1ee3p \u0111\u1ec3 m&ocirc; t\u1ea3 ho\u1eb7c tr&igrave;nh di\u1ec5n nh\u1eefng t&iacute;nh n\u0103ng c\u1ee7a jQuery v\u1edbi nh\u1eefng ng\u01b0\u1eddi n&agrave;o \u0111&oacute; ch\u01b0a t\u1eebng nghi&ecirc;n c\u1ee9u v\u1ec1 kh&aacute;i ni\u1ec7m \u0111\u1ed1i t\u01b0\u1ee3ng trong JavaScript. \u0110\u1eb7c \u0111i\u1ec3m c\u01a1 b\u1ea3n v\u1ec1 thi\u1ebft k\u1ebf \u0111\u1ed1i t\u01b0\u1ee3ng trong JavaScript l&agrave; khi ng\u01b0\u1eddi s\u1eed d\u1ee5ng khai b&aacute;o ho\u1eb7c kh\u1edfi t\u1ea1o 1 function, th&igrave; function \u0111&oacute; s\u1ebd l&agrave; 1 class \u1edf ch\u1ebf \u0111\u1ed9 m\u1eb7c \u0111\u1ecbnh, v&agrave; sau \u0111&oacute; s\u1ebd \u0111\u01b0\u1ee3c d&ugrave;ng nh\u01b0 1 \u0111\u1ed1i t\u01b0\u1ee3ng.<\/p>\n<p>\n\tB&ecirc;n c\u1ea1nh \u0111&oacute;, ch&uacute;ng ta c&oacute; th\u1ec3 s\u1eed d\u1ee5ng to&aacute;n t\u1eed new \u0111\u1ec3 t\u1ea1o b\u1ea3n copy c\u1ee7a c&ugrave;ng class. V&agrave; n&oacute; ch\u1ec9 x\u1ea3y ra khi ng\u01b0\u1eddi d&ugrave;ng thay th\u1ebf b\u1eb1ng t\u1eeb kh&oacute;a class quen thu\u1ed9c, JavaScript s\u1ebd \u01b0u ti&ecirc;n t\u1eeb kh&oacute;a function h\u01a1n. L\u01b0u &yacute; r\u1eb1ng class trong JavaScript c&oacute; th\u1ec3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng d\u01b0\u1edbi d\u1ea1ng 1 class \u0111&atilde; \u0111\u01b0\u1ee3c k\u1ebf th\u1eeba &ndash; <strong>inhertited<\/strong> t\u1eeb class kh&aacute;c (b\u1eb1ng c&aacute;ch th\u1ef1c hi\u1ec7n c&aacute;c <strong>prototype inheritance<\/strong>) ho\u1eb7c \u0111\u01b0\u1ee3c x&acirc;y d\u1ef1ng ch&iacute;nh x&aacute;c nh\u01b0 1 function t\u1eeb trong m&atilde; ngu\u1ed3n. N&oacute;i theo c&aacute;ch ng\u1eafn g\u1ecdn th&igrave; trong JavaScript, t\u1ea5t c\u1ea3 \u0111\u1ec1u l&agrave; \u0111\u1ed1i t\u01b0\u1ee3ng.<\/p>\n<p>\n\tM\u1eb7t kh&aacute;c, <strong>function closures<\/strong> th\u01b0\u1eddng \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng v\u1edbi c&aacute;c event trong <strong>jQuery<\/strong>. 1 <strong>function closure<\/strong> \u0111i\u1ec3n h&igrave;nh ch&iacute;nh l&agrave; 1 lo\u1ea1i function kh&ocirc;ng c&oacute; t&ugrave;y bi\u1ebfn ho\u1eb7c t&ecirc;n b\u1ea5t k\u1ef3, m&agrave; \u0111\u01a1n gi\u1ea3n ch\u1ec9 l&agrave; 1 \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee7a function \u0111&oacute;.<\/p>\n<h2>\n\tC&aacute;c \u0111\u1ed1i t\u01b0\u1ee3ng ch&iacute;nh trong jQuery:<\/h2>\n<p>\n\tV\u1ec1 m\u1eb7t b\u1ea3n ch\u1ea5t, th&igrave; c&aacute;c \u0111\u1ed1i t\u01b0\u1ee3ng ch&iacute;nh c\u1ee7a jQuery \u0111\u1ec1u \u0111\u01b0\u1ee3c \u0111\u1ecbnh ngh\u0129a v\u1edbi t\u1eeb kh&oacute;a function nh\u01b0 1 \u0111\u1ed1i t\u01b0\u1ee3ng v\u1edbi t&ecirc;n \u0111\u1ecbnh danh l&agrave; $, v&agrave; ch&uacute;ng c&ograve;n t\u01b0\u01a1ng \u0111\u01b0\u01a1ng v\u1edbi <strong>global function<\/strong>, ch\u1eb3ng h\u1ea1n nh\u01b0 window.$. V&iacute; d\u1ee5, trong <strong>JavaScript<\/strong> khi ch&uacute;ng ta m\u1edf r\u1ed9ng m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng window n&agrave;o \u0111&oacute; (ph\u1ea3i c&oacute; s\u1eb5n trong tr&igrave;nh duy\u1ec7t t\u1eeb kh&acirc;u thi\u1ebft k\u1ebf), ho\u1eb7c n&oacute;i theo c&aacute;ch kh&aacute;c l&agrave; g&aacute;n th&ecirc;m <strong>function member<\/strong> v\u1edbi (.) t\u1edbi \u0111\u1ed1i t\u01b0\u1ee3ng <strong>window<\/strong>, v&agrave; c\u0169ng c&oacute; ngh\u0129a r\u1eb1ng ch&uacute;ng ta c&oacute; th\u1ec3 g\u1ecdi \u0111\u1ed1i t\u01b0\u1ee3ng \u0111&oacute; b\u1eb1ng <strong>window.myobj();<\/strong> ho\u1eb7c \u0111\u01a1n gi\u1ea3n l&agrave; <strong>myobj();<\/strong>. B\u1edfi v&igrave; v\u1edbi m\u1ed7i function \u0111\u01b0\u1ee3c g\u1eafn v\u1edbi <strong>object window<\/strong> \u0111\u1ec1u c&oacute; th\u1ec3 \u0111\u01b0\u1ee3c g\u1ecdi t\u1eeb global scope trong to&agrave;n b\u1ed9 script. C&ograve;n t\u1eeb b&ecirc;n trong, c&aacute;c \u0111\u1ed1i t\u01b0\u1ee3ng c\u1ee7a jQuery s\u1ebd \u0111\u01b0\u1ee3c t\u1ea1o nh\u01b0 sau:<\/p>\n<div style=\"margin: 10px auto;overflow:auto;background-\ncolor:#f7f8f8;width:600px;padding:10px; border:1px dashed\n#2f6fab;line-height:1.1em;  font-family:Courier New; font-\nstyle:italic;\"><br \/>\n\tvar jQuery = window.jQuery = window.$ = function(selector, context)<br \/>\n\t{<br \/>\n\t\/\/ &#8230;<br \/>\n\t\/\/ other internal initialization code goes here<br \/>\n\t};<\/div>\n<p>\n\tThu\u1eadt to&aacute;n khai b&aacute;o ch&iacute;nh x&aacute;c n&agrave;y cho ph&eacute;p ng\u01b0\u1eddi d&ugrave;ng d\u1ec5 d&agrave;ng chuy\u1ec3n \u0111\u1ebfn c&aacute;c \u0111\u1ed1i t\u01b0\u1ee3ng jQuery ch&iacute;nh qua t&ecirc;n jQuery ho\u1eb7c k&yacute; hi\u1ec7u $. C&aacute;c b\u1ea1n c\u1ea7n bi\u1ebft r\u1eb1ng <strong>jQuery<\/strong>, <strong>window.jQuery<\/strong>, <strong>window.$<\/strong> ho\u1eb7c \u0111\u01a1n gi\u1ea3n ch\u1ec9 l&agrave; $ \u0111\u1ec1u c&oacute; th\u1ec3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng thay th\u1ebf cho nhau, b\u1edfi v&igrave; qu&aacute; tr&igrave;nh khai b&aacute;o \u1edf ph\u1ea7n \u0111\u1ea7u c\u1ee7a b\u1ed9 m&atilde;, v&agrave; n&oacute; c&oacute; li&ecirc;n quan \u0111\u1ebfn to&agrave;n b\u1ed9 \u0111\u1ed1i t\u01b0\u1ee3ng.<\/p>\n<p>\n\t1 \u0111i\u1ec3m n\u1eefa c\u1ea7n l\u01b0u &yacute; l&agrave; tham s\u1ed1 selector v&agrave; context c\u1ee7a jQuery c&oacute; ch\u1ee9c n\u0103ng ho\u1ea1t \u0111\u1ed9ng nh\u01b0 1 object b&igrave;nh th\u01b0\u1eddng. <strong>Selector <\/strong>th\u01b0\u1eddng l&agrave; chu\u1ed7i &ndash; <strong>string <\/strong>c&oacute; ch\u1ee9c n\u0103ng l\u1ef1a ch\u1ecdn 1 s\u1ed1 l\u01b0\u1ee3ng nh\u1ea5t \u0111\u1ecbnh c&aacute;c element t\u1eeb <strong>DOM <\/strong>&ndash; <strong>Document Object Model<\/strong>. Ho\u1eb7c c\u0169ng c&oacute; th\u1ec3 l&agrave; \u0111\u1ed1i t\u01b0\u1ee3ng <strong>this <\/strong>(<strong>self reference<\/strong>). Tham s\u1ed1 <strong>jQuery selector<\/strong> c&oacute; th\u1ec3 ch\u1ea5p nh\u1eadn nhi\u1ec1u gi&aacute; tr\u1ecb gi\u1ed1ng nhau trong tr\u01b0\u1eddng h\u1ee3p ng\u01b0\u1eddi d&ugrave;ng mu\u1ed1n s\u1eed d\u1ee5ng trong qu&aacute; tr&igrave;nh kh\u1edfi t\u1ea1o style c\u1ee7a <strong>CSS<\/strong>. V&iacute; d\u1ee5, c&aacute;c b\u1ea1n h&atilde;y tham kh\u1ea3o qu&aacute; tr&igrave;nh g\u1ecdi \u0111\u1ed1i t\u01b0\u1ee3ng trong <strong>jQuery <\/strong>nh\u01b0 sau:<\/p>\n<div style=\"margin: 10px auto;overflow:auto;background-\ncolor:#f7f8f8;width:600px;padding:10px; border:1px dashed\n#2f6fab;line-height:1.1em;  font-family:Courier New; font-\nstyle:italic;\"><\/p>\n<pre>\r\n<code>\/\/ Select all elements of class: &quot;someClass&quot; and\r\n\/\/ apply a red border to all of them\r\njQuery(&quot;.someClass&quot;).css(&quot;border&quot;, &quot;1px solid red&quot;);<\/code><\/pre>\n<p>\n\t\t\/\/ Select an element with id: someId and insert dynamic html into it jQuery(&quot;#someId&quot;).html(&quot;&lt;b&gt;So Bold!&lt;\/b&gt;&quot;);<\/p>\n<p>\n\t\t<code>\/\/ Exactly the same as above, but using $ $(&quot;#someId&quot;).html(&quot;&lt;b&gt;So Bold!&lt;\/b&gt;&quot;);<\/code><\/p>\n<\/div>\n<p>\n\tR\u1ea5t ng\u1eafn g\u1ecdn v&agrave; \u0111\u01a1n gi\u1ea3n, c&aacute;c b\u1ea1n kh&ocirc;ng c&ograve;n ph\u1ea3i t\u1ef1 vi\u1ebft <strong>function <\/strong>document.getElementById v\u1ed1n r\u1ea5t d\u1ec5 g&acirc;y nh\u1ea7m l\u1eabn. Ch\u1ec9 v\u1edbi 1 d&ograve;ng l\u1ec7nh, jQuery s\u1ebd ch\u1ecdn t\u1ea5t c\u1ea3 c&aacute;c th&agrave;nh ph\u1ea7n theo y&ecirc;u c\u1ea7u b\u1eb1ng c&aacute;c r&agrave; so&aacute;t to&agrave;n b\u1ed9 <strong>DOM <\/strong>v&agrave; &aacute;p d\u1ee5ng &ldquo;hi\u1ec7u l\u1ef1c&rdquo;. M\u1ed9t \u0111i\u1ec3m r\u1ea5t \u0111&aacute;ng ch&uacute; &yacute; n\u1eefa c\u1ee7a jQuery l&agrave; ch\u1ebf \u0111\u1ed9 t\u01b0\u01a1ng th&iacute;ch v\u1edbi t\u1ea5t c\u1ea3 c&aacute;c tr&igrave;nh duy\u1ec7t hi\u1ec7n nay.<\/p>\n<div id=\"VietAd\">\n<h2>\n\t\t\u0110i\u1ec3m m\u1ea5u ch\u1ed1t c\u1ee7a plugin jQuery:<\/h2>\n<p>\n\t\tT\u1ea1i \u0111&acirc;y, ch&uacute;ng ta s\u1ebd b\u1eaft \u0111\u1ea7u v\u1edbi v&iacute; d\u1ee5 \u0111\u01a1n gi\u1ea3n v\u1ec1 <strong>plugin jQuery<\/strong> kh&aacute; ph\u1ed5 bi\u1ebfn. C\u1ee5 th\u1ec3, khi m\u1edbi b\u1eaft \u0111\u1ea7u t&igrave;m hi\u1ec3u v\u1ec1 1 ng&ocirc;n ng\u1eef l\u1eadp tr&igrave;nh ho\u1eb7c n\u1ec1n t\u1ea3ng <strong>framework <\/strong>n&agrave;o \u0111&oacute; th&igrave; ch&uacute;ng ta \u0111\u1ec1u ph\u1ea3i bi\u1ebft \u0111\u01b0\u1ee3c \u0111i\u1ec3m m\u1ea5u ch\u1ed1t c\u1ee7a v\u1ea5n \u0111\u1ec1. Trong th\u1eddi \u0111i\u1ec3m jQuery ch\u01b0a xu\u1ea5t hi\u1ec7n, nh\u1eefng l\u1eadp tr&igrave;nh vi&ecirc;n <strong>JavaScript <\/strong>th\u01b0\u1eddng c&oacute; th&oacute;i quen th\u1ef1c thi c&aacute;c \u0111o\u1ea1n m&atilde;, ch\u1ee9c n\u0103ng quan tr\u1ecdng trong <strong>function<\/strong> <strong>window.onload<\/strong> nh\u01b0 minh h\u1ecda d\u01b0\u1edbi \u0111&acirc;y:<\/p>\n<div style=\"margin: 10px auto;overflow:auto;background-\ncolor:#f7f8f8;width:600px;padding:10px; border:1px dashed\n#2f6fab;line-height:1.1em;  font-family:Courier New; font-\nstyle:italic;\"><\/p>\n<pre>\r\n<code>\/\/ Override the onload event\r\nwindow.onload = function()\r\n{\r\n    \/\/ the page finished loading, do something here...\r\n}<\/code><\/pre>\n<\/p><\/div>\n<p>\n\t\tTh\u1ef1c ch\u1ea5t, \u0111o\u1ea1n m&atilde; tr&ecirc;n ch\u1ec9 th\u1ef1c hi\u1ec7n nhi\u1ec7m v\u1ee5 ghi \u0111&egrave; l&ecirc;n <strong>event onload<\/strong> c\u1ee7a th\u1ebb <strong>&lt;body&gt;<\/strong> trong <strong>HTML<\/strong>, ho\u1eb7c hi\u1ec3u n&ocirc;m na r\u1eb1ng \u0111o\u1ea1n m&atilde; c\u1ee7a ch&uacute;ng ta ch\u1ec9 \u0111\u01b0\u1ee3c th\u1ef1c thi khi trang web ho&agrave;n t\u1ea5t qu&aacute; tr&igrave;nh t\u1ea3i. Trong nhi\u1ec1u tr\u01b0\u1eddng h\u1ee3p th&igrave; vi\u1ec7c n&agrave;y l\u1ea1i r\u1ea5t kh&oacute; kh\u0103n v&igrave; c&oacute; nh\u1eefng trang c\u1ea7n nhi\u1ec1u th\u1eddi gian \u0111\u1ec3 t\u1ea3i h\u1ebft n\u1ed9i dung, ho\u1eb7c qu&aacute; tr&igrave;nh t\u1ea3i b\u1ecb gi&aacute;n \u0111o\u1ea1n do c\u1ea5u tr&uacute;c duy\u1ec7t d\u1eef li\u1ec7u kh&aacute;c nhau.<\/p>\n<p>\n\t\tT\u1ea1i \u0111&acirc;y, ch&uacute;ng ta kh&ocirc;ng mu\u1ed1n compile v&agrave; th\u1ef1c thi b\u1ea5t k\u1ef3 \u0111o\u1ea1n m&atilde; <strong>JavaScript <\/strong>n&agrave;o tr&ecirc;n trang \u0111ang \u0111\u01b0\u1ee3c t\u1ea3i. Ki\u1ebfn tr&uacute;c b&ecirc;n trong c\u1ee7a jQuery t\u1eadn d\u1ee5ng t\u1ed1i \u0111a \u01b0u \u0111i\u1ec3m c\u1ee7a event <strong>window.onload<\/strong>, nh\u01b0ng tr\u01b0\u1edbc \u0111&oacute; c\u0169ng th\u1ef1c hi\u1ec7n vi\u1ec7c ki\u1ec3m tra to&agrave;n b\u1ed9 DOM \u0111&atilde; \u0111\u01b0\u1ee3c t\u1ea3i hay ch\u01b0a, v&igrave; quy tr&igrave;nh n&agrave;y r\u1ea5t quan tr\u1ecdng.<\/p>\n<p>\n\t\tTuy nhi&ecirc;n, nh\u1eefng y\u1ebfu t\u1ed1 tr&ecirc;n l\u1ea1i ch\u01b0a \u0111\u1ee7 \u0111\u1ec3 jQuery &ldquo;bi\u1ebft&rdquo; trang web \u0111&atilde; \u0111\u01b0\u1ee3c t\u1ea3i ho&agrave;n t\u1ea5t hay ch\u01b0a, m&agrave; ch&uacute;ng ta ph\u1ea3i ch\u1eafc ch\u1eafn r\u1eb1ng <strong>DOM <\/strong>\u0111&atilde; \u0111\u01b0\u1ee3c &ldquo;x&acirc;y d\u1ef1ng&rdquo; \u0111\u1ea7y \u0111\u1ee7 &ndash; th\u1ef1c hi\u1ec7n b\u1eb1ng c&aacute;ch &ldquo;l\u1eafng nghe&rdquo; t&iacute;n hi\u1ec7u <strong>DOMContentLoaded <\/strong>tr&ecirc;n h\u1ea7u h\u1ebft t\u1ea5t c\u1ea3 c&aacute;c tr&igrave;nh duy\u1ec7t. Nh\u01b0ng th\u1eadt l&agrave; may m\u1eafn v&igrave; ch&uacute;ng ta kh&ocirc;ng c\u1ea7n ph\u1ea3i th\u1ef1c hi\u1ec7n c&ocirc;ng \u0111o\u1ea1n ki\u1ec3m tra nh\u01b0 tr&ecirc;n, jQuery \u0111&atilde; \u0111\u1ea3m nh\u1eadn to&agrave;n b\u1ed9 vi\u1ec7c n&agrave;y.<\/p>\n<p>\n\t\t\u0110\u1ec3 h\u1ed7 tr\u1ee3 ng\u01b0\u1eddi s\u1eed d\u1ee5ng, jQuery \u0111&atilde; cung c\u1ea5p 1 method m\u1edbi v\u1edbi t&ecirc;n g\u1ecdi <strong>ready <\/strong>m&agrave; ch&uacute;ng ta c&oacute; th\u1ec3 g\u1ecdi ra t\u1eeb ch&iacute;nh \u0111\u1ed1i t\u01b0\u1ee3ng ch&iacute;nh c\u1ee7a jQuery. C\u1ee5 th\u1ec3, khi vi\u1ebft plugin jQuery, c&aacute;c b\u1ea1n s\u1eed d\u1ee5ng <strong>function ready <\/strong>n&agrave;y \u0111\u1ec3 ki\u1ec3m tra xem h\u1ec7 th\u1ed1ng \u0111&atilde; s\u1eb5n s&agrave;ng \u0111\u1ec3 th\u1ef1c thi \u0111o\u1ea1n m&atilde; \u0111&oacute; hay ch\u01b0a. L\u01b0u &yacute; r\u1eb1ng \u0111&acirc;y kh&ocirc;ng ph\u1ea3i m&atilde; plugin ch&iacute;nh m&agrave; l&agrave; \u0111i\u1ec3m m\u1ea5u ch\u1ed1t c\u1ee7a plugin. Ho\u1eb7c hi\u1ec3u n&ocirc;m na r\u1eb1ng \u0111&acirc;y l&agrave; phi&ecirc;n b\u1ea3n jQuery c\u1ee7a <strong>function window.onload:<\/strong><\/p>\n<div style=\"margin: 10px auto;overflow:auto;background-\n\ncolor:#f7f8f8;width:600px;padding:10px; border:1px dashed \n\n#2f6fab;line-height:1.1em;  font-family:Courier New; font-\n\nstyle:italic;\"><\/p>\n<pre>\r\n&lt;script type = &quot;text\/javascript&quot;&gt;\r\n \/\/ Define the entry point\r\n $(document).ready(function()\r\n {\r\n \/\/ The DOM (document object model) is constructed\r\n \/\/ We will initialize and run our plugin here\r\n });\r\n &lt;\/script&gt;<\/pre>\n<\/p><\/div>\n<p>\n\t\tV&iacute; d\u1ee5 t\u1ea1i \u0111&acirc;y, ch&uacute;ng ta s\u1ebd vi\u1ebft 1 plugin v\u1edbi t&ecirc;n g\u1ecdi l&agrave; <strong>Shuffle<\/strong>, v&agrave; plugin n&agrave;y c&oacute; 2 ch\u1ee9c n\u0103ng ri&ecirc;ng r\u1ebd \u0111\u1ec3 kh\u1edfi t\u1ea1o c\u0169ng nh\u01b0 th\u1ef1c thi ph\u1ea7n m&atilde; c\u01a1 b\u1ea3n, c&oacute; d\u1ea1ng nh\u01b0 sau:<\/p>\n<div style=\"margin: 10px auto;overflow:auto;background-\n\ncolor:#f7f8f8;width:600px;padding:10px; border:1px dashed \n\n#2f6fab;line-height:1.1em;  font-family:Courier New; font-\n\nstyle:italic;\"><\/p>\n<pre>\r\n&lt;script type = &quot;text\/javascript&quot;&gt;\r\n \/\/ One way to initialize plugin code\r\n $(document).ready(function()\r\n {\r\n jQuery.Shuffle.initialize( &quot;monalisa.jpg&quot;, 5, 8, 67, 1500);\r\n jQuery.Shuffle.run();\r\n });\r\n &lt;\/script&gt;<\/pre>\n<\/p><\/div>\n<p>\n\t\tNh\u01b0 th\u01b0\u1eddng l\u1ec7 th&igrave; b\u1ea5t k\u1ef3 \u0111o\u1ea1n m&atilde; n&agrave;o c\u0169ng c&oacute; th\u1ec3 \u0111\u01b0\u1ee3c t&ugrave;y bi\u1ebfn d\u1ef1a v&agrave;o nhu c\u1ea7u c\u1ee7a ng\u01b0\u1eddi s\u1eed d\u1ee5ng. C&uacute; ph&aacute;p s\u1eed d\u1ee5ng d\u01b0\u1edbi \u0111&acirc;y \u0111\u01b0\u1ee3c coi l&agrave; ph\u1ed5 bi\u1ebfn, b\u1edfi n&oacute; \u0111\u01b0\u1ee3c &aacute;p d\u1ee5ng b\u1edfi ph\u1ea7n l\u1edbn l\u1eadp tr&igrave;nh vi&ecirc;n <strong>jQuery<\/strong>, tuy nhi&ecirc;n l\u1ea1i r\u1ea5t d\u1ec5 g&acirc;y nh\u1ea7m l\u1eabn \u0111\u1eb7c bi\u1ec7t l&agrave; v\u1edbi nh\u1eefng ng\u01b0\u1eddi m\u1edbi b\u1eaft \u0111\u1ea7u t&igrave;m hi\u1ec3u v\u1ec1 c\u1ea3 <strong>JavaScript <\/strong>v&agrave; <strong>jQuery<\/strong>:<\/p>\n<div style=\"margin: 10px auto;overflow:auto;background-\n\ncolor:#f7f8f8;width:600px;padding:10px; border:1px dashed \n\n#2f6fab;line-height:1.1em;  font-family:Courier New; font-\n\nstyle:italic;\"><\/p>\n<pre>\r\n&lt;script type = &quot;text\/javascript&quot;&gt;\r\n (function($){ ... })(jQuery);\r\n &lt;\/script&gt;<\/pre>\n<\/p><\/div>\n<p>\n\t\t\u0110&acirc;y l&agrave; tr\u01b0\u1eddng h\u1ee3p r\u1ea5t hay g\u1eb7p trong tr\u1ef1c t\u1ebf, d\u1ea5u 3 ch\u1ea5m th\u01b0\u1eddng \u0111\u01b0\u1ee3c thay th\u1ebf b\u1eb1ng \u0111o\u1ea1n m&atilde; c\u1ea7n th\u1ef1c thi. C\u1ee5 th\u1ec3 t\u1ea1i \u0111&acirc;y, c&aacute;c b\u1ea1n c\u1ea7n tham kh\u1ea3o v\u1ec1 \u0111\u1ecbnh ngh\u0129a c\u1ee7a function anonymous (hay c&ograve;n g\u1ecdi l&agrave; <strong>function closure<\/strong>) v\u1edbi tham s\u1ed1 \u0111i k&egrave;m l&agrave; $. V&agrave; ph\u1ea7n ch\u1ee9c n\u0103ng n&agrave;y \u0111\u01b0\u1ee3c bao trong d\u1ea5u ngo\u1eb7c \u0111\u01a1n, v&igrave; sao v\u1eady? Function trong v&iacute; d\u1ee5 n&agrave;y th\u1ef1c ch\u1ea5t l&agrave; 1 function anonymous kh&ocirc;ng tham chi\u1ebfu \u0111\u1ebfn ch&iacute;nh b\u1ea3n th&acirc;n n&oacute;, nh\u01b0ng v&igrave; \u1edf trong &ldquo;ph\u1ea1m vi&rdquo; c\u1ee7a d\u1ea5u ngo\u1eb7c \u0111\u01a1n n&ecirc;n JavaScript s\u1ebd cho ph&eacute;p ng\u01b0\u1eddi d&ugrave;ng tham chi\u1ebfu \u0111\u1ebfn b\u1ea5t k\u1ef3 <strong>function anonymous<\/strong> \u0111&atilde; \u0111\u01b0\u1ee3c t\u1ea1o.<\/p>\n<p>\n\t\tB&ecirc;n c\u1ea1nh \u0111&oacute;, ch&uacute;ng ta c&oacute; th\u1ec3 th&ecirc;m 1 d\u1ea5u ch\u1ea5m sau khi ph\u1ea7n kh\u1edfi t\u1ea1o c\u1ee7a function \u0111\u01b0\u1ee3c \u0111\u1eb7t trong d\u1ea5u ngo\u1eb7c \u0111\u01a1n, v&agrave; g\u1ecdi ra 1 <strong>function member <\/strong>\u0111\u01b0\u1ee3c h\u1ed7 tr\u1ee3 b\u1edfi function n&agrave;y. Ti\u1ebfp theo, th&ecirc;m d\u1ea5u ch\u1ea5m \u1edf ph&iacute;a cu\u1ed1i m\u1ec7nh \u0111\u1ec1 \u0111&oacute; v&agrave; g\u1ecdi ti\u1ebfp 1 <strong>function member<\/strong> kh&aacute;c c&oacute; th\u1ec3 \u0111\u01b0\u1ee3c th\u1ef1c thi d\u1ef1a tr&ecirc;n \u0111\u1ecbnh d\u1ea1ng c\u1ee7a \u0111\u1ed1i t\u01b0\u1ee3ng \u0111\u01b0\u1ee3c tr\u1ea3 v\u1ec1 b\u1edfi function tr\u01b0\u1edbc. T&iacute;nh n\u0103ng function chaining n&agrave;y c\u1ee7a <strong>JavaScript <\/strong>kh&aacute; ph\u1ed5 bi\u1ebfn v&agrave; \u0111\u01b0\u1ee3c &aacute;p d\u1ee5ng trong nhi\u1ec1u ng&ocirc;n ng\u1eef l\u1eadp tr&igrave;nh kh&aacute;c, ch\u1eb3ng h\u1ea1n nh\u01b0 <strong>Perl<\/strong>. \u0110&oacute; l&agrave; 1 d\u1ea1ng t&iacute;nh n\u0103ng c\u1ee7a ng\u1ed3n ng\u1eef k\u1ecbch b\u1ea3n v&igrave; n&oacute; gi&uacute;p cho ph\u1ea7n m&atilde; c\u1ee7a ng\u01b0\u1eddi d&ugrave;ng tr\u1edf n&ecirc;n ng\u1eafn g\u1ecdn, d\u1ec5 qu\u1ea3n l&yacute; v&agrave; tr\u1ef1c quan h\u01a1n.<\/p>\n<p>\n\t\tDo v\u1eady, b\u1eb1ng c&aacute;ch \u0111\u1eb7t function <strong>anonymous <\/strong>trong d\u1ea5u ngo\u1eb7c \u0111\u01a1n th&igrave; ch&uacute;ng ta c&oacute; th\u1ec3 d\u1ec5 d&agrave;ng tham chi\u1ebfu \u0111\u1ebfn ph\u1ea7n b\u1ed9 nh\u1edb c\u1ee7a ch&iacute;nh function \u0111&oacute; m&agrave; kh&ocirc;ng c\u1ea7n ph\u1ea3i qua t&ecirc;n &ndash; m&agrave; th\u1ef1c ra c\u0169ng kh&ocirc;ng th\u1ec3 l&agrave;m \u0111\u01b0\u1ee3c v&igrave; function kh&ocirc;ng c&oacute; t&ecirc;n.<\/p>\n<p>\n\t\tM\u1eb7t kh&aacute;c, c&aacute;c b\u1ea1n c&ograve;n c&oacute; th\u1ec3 g\u1ecdi <strong>function <\/strong>\u0111&oacute; b\u1eb1ng m\u1ec7nh \u0111\u1ec1 \u0111\u01b0\u1ee3c d&ugrave;ng \u0111\u1ec3 t\u1ea1o function. V&agrave; \u0111&oacute; c\u0169ng ch&iacute;nh l&agrave; c&aacute;ch l&agrave;m c\u1ee7a ch&uacute;ng ta t\u1ea1i \u0111&acirc;y. C&aacute;c function kh&ocirc;ng c&oacute; t&ecirc;n \u0111\u01b0\u1ee3c \u0111\u1ecbnh ngh\u0129a, \u0111\u1eb7t trong d\u1ea5u ngo\u1eb7c \u0111\u01a1n v&agrave; s\u1ebd \u0111\u01b0\u1ee3c g\u1ecdi ngay sau \u0111&oacute;. \u0110&acirc;y ch\u1ec9 l&agrave; 1 v&iacute; d\u1ee5 \u0111\u01a1n gi\u1ea3n v\u1ec1 c&aacute;ch s\u1eed d\u1ee5ng c\u1ee7a function closure m&agrave; ch&uacute;ng ta c&oacute; th\u1ec3 d\u1ec5 d&agrave;ng nh\u1eadn ra trong nhi\u1ec1u ph\u1ea7n m&atilde; ngu\u1ed3n jQuery v&agrave; <strong>JavaScript <\/strong>n&acirc;ng cao.<\/p>\n<p>\n\t\tNh\u1eefng l&yacute; do \u0111\u1ec3 l&agrave;m nh\u01b0 v\u1eady l&agrave; g&igrave;? Th\u1ee9 nh\u1ea5t l&agrave; \u0111\u1ec3 r&uacute;t g\u1ecdn t\u1ed1i \u0111a \u0111\u1ed9 d&agrave;i c\u1ee7a code, ti\u1ebfp theo l&agrave; \u1ea9n to&agrave;n b\u1ed9 c&aacute;c tham s\u1ed1 \u0111\u01b0\u1ee3c kh\u1edfi \u0111\u1ea7u b\u1eb1ng $ kh\u1ecfi m&ocirc; h&igrave;nh <strong>global scope<\/strong> t\u1ed5ng th\u1ec3. B&ecirc;n c\u1ea1nh \u0111&oacute;, \u0111\u1ed1i v\u1edbi nh\u1eefng tr\u01b0\u1eddng h\u1ee3p ch&uacute;ng ta d&ugrave;ng nhi\u1ec1u n\u1ec1n t\u1ea3ng framework kh&aacute;c nhau th&igrave; vi\u1ec7c s\u1eed d\u1ee5ng k&yacute; hi\u1ec7u $ \u0111\u1ed1i v\u1edbi object function trong m&ocirc; h&igrave;nh c\u1ee7a to&agrave;n b\u1ed9 ch\u01b0\u01a1ng tr&igrave;nh r\u1ea5t d\u1ec5 g&acirc;y nh\u1ea7m l\u1eabn.<\/p>\n<p>\n\t\tNh\u01b0ng trong th\u1ef1c t\u1ebf th&igrave; \u0111i\u1ec1u \u0111&oacute; l\u1ea1i ho&agrave;n to&agrave;n ph\u1ee5 thu\u1ed9c v&agrave;o ho&agrave;n c\u1ea3nh v&agrave; s\u1ef1 l\u1ef1a ch\u1ecdn ch&iacute;nh c\u1ee7a ng\u01b0\u1eddi ph&aacute;t tri\u1ec3n. C\u1ee5 th\u1ec3, trong ph\u1ea7n ti\u1ebfp theo c\u1ee7a b&agrave;i h\u01b0\u1edbng d\u1eabn th&igrave; ch&uacute;ng t&ocirc;i kh&ocirc;ng h\u1ec1 s\u1eed d\u1ee5ng b\u1ea5t c\u1ee9 framework ho\u1eb7c m&atilde; b\u1ed5 sung b&ecirc;n ngo&agrave;i, c&uacute; ph&aacute;p chung c&oacute; th\u1ec3 h\u01a1i lo\u1eb1ng ngo\u1eb1ng v&agrave; kh&oacute; hi\u1ec3u, nh\u01b0ng t\u1ec9 l\u1ec7 xung \u0111\u1ed9t v&agrave; l\u1ed7i ph&aacute;t sinh s\u1ebd \u0111\u01b0\u1ee3c \u0111\u1ea3m b\u1ea3o \u1edf m\u1ee9c th\u1ea5p nh\u1ea5t. C&uacute; ph&aacute;p c\u01a1 b\u1ea3n chung nh\u01b0 sau:<\/p>\n<div style=\"margin: 10px auto;overflow:auto;background-\n\ncolor:#f7f8f8;width:600px;padding:10px; border:1px dashed \n\n#2f6fab;line-height:1.1em;  font-family:Courier New; font-\n\nstyle:italic;\"><\/p>\n<pre>\r\n&lt;script type = &quot;text\/javascript&quot;&gt;\r\n jQuery.func = doSomething;\r\n jQuery.fn.func = doSomethingElse;\r\n &lt;\/script&gt;<\/pre>\n<\/p><\/div>\n<p>\n\t\tV\u1edbi <strong>doSomething <\/strong>v&agrave; <strong>doSomethingElse <\/strong>\u0111\u01a1n gi\u1ea3n ch\u1ec9 l&agrave; nh\u1eefng \u0111\u1ed1i t\u01b0\u1ee3ng function \u0111&atilde; \u0111\u01b0\u1ee3c \u0111\u1ecbnh ngh\u0129a v&agrave; kh\u1edfi t\u1ea1o tr\u01b0\u1edbc \u0111&oacute;, nh\u01b0ng n\u1ebfu l&agrave;m nh\u01b0 v\u1eady th&igrave; ch&uacute;ng ta \u0111\u1ed3ng th\u1eddi c\u0169ng m\u1ea5t \u0111i kh\u1ea3 n\u0103ng k\u1ebft h\u1ee3p, x&acirc;u chu\u1ed7i plugin jQuery v\u1edbi c&aacute;c function <strong>API <\/strong>\u0111&atilde; c&oacute; s\u1eb5n. Cu\u1ed1i c&ugrave;ng, n\u1ebfu c&aacute;c b\u1ea1n mu\u1ed1n plugin c&oacute; kh\u1ea3 n\u0103ng chaining th&igrave; ph\u1ea3i s\u1eed d\u1ee5ng function <strong>sell-calling<\/strong> nh\u01b0 \u0111&atilde; gi\u1ea3i th&iacute;ch \u1edf tr&ecirc;n. N\u1ebfu k\u1ebft h\u1ee3p ch\u1ee9c n\u0103ng chaining c\u1ee7a plugin v\u1edbi m&atilde; c\u1ee7a nhi\u1ec1u d\u1ef1 &aacute;n kh&aacute;c, c&aacute;c b\u1ea1n ch\u1ec9 c\u1ea7n gh&eacute;p \u0111\u1ed1i t\u01b0\u1ee3ng v&agrave; bi\u1ebfn c\u1ea7n thi\u1ebft t\u1edbi \u0111\u1ed1i t\u01b0\u1ee3ng <strong>$<\/strong>. ho\u1eb7c <strong>$.fn<\/strong>. L\u01b0u &yacute; r\u1eb1ng trong jQuery, m\u1ed9t s\u1ed1 k&yacute; t\u1ef1 nh\u01b0 d\u1ea5u $ ho\u1eb7c m\u1ed9t v&agrave;i tr\u01b0\u1eddng h\u1ee3p \u0111\u1eb7c bi\u1ec7t nh\u01b0 <strong>window.$<\/strong> c&oacute; th\u1ec3 \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng thay th\u1ebf nhau, \u0111\u1ed3ng th\u1eddi c&ograve;n \u0111\u01b0\u1ee3c d&ugrave;ng \u0111\u1ec3 \u0111\u1ec1 c\u1eadp ch&iacute;nh x&aacute;c t\u1edbi 1 \u0111\u1ed1i t\u01b0\u1ee3ng b\u1ea5t k\u1ef3 n&agrave;o \u0111&oacute; \u0111&atilde; l\u01b0u trong b\u1ed9 nh\u1edb. Trong t&agrave;i li\u1ec7u h\u01b0\u1edbng d\u1eabn c\u1ee5 th\u1ec3 c\u1ee7a jQuery c\u0169ng \u0111&atilde; ch\u1ec9 ra r\u1eb1ng ch&uacute;ng ta n&ecirc;n s\u1eed d\u1ee5ng \u0111\u1ed1i t\u01b0\u1ee3ng jQuery thay cho d\u1ea5u $. V&igrave; k&yacute; hi\u1ec7u $ ph\u1ea3i \u0111\u01b0\u1ee3c \u1ea9n, cho n&ecirc;n ch\u1ec9 s\u1eed d\u1ee5ng trong to&agrave;n b\u1ed9 c\u1ea5u tr&uacute;c ri&ecirc;ng c\u1ee7a jQuery m&agrave; th&ocirc;i, v&agrave; tuy\u1ec7t \u0111\u1ed1i kh&ocirc;ng \u0111\u01b0\u1ee3c &ldquo;ti\u1ebfp x&uacute;c&rdquo; v\u1edbi b\u1ea5t k\u1ef3 tham s\u1ed1 $ n&agrave;o t\u1edbi ph\u1ea7n <strong>implementators <\/strong>c\u1ee7a <strong>plugin<\/strong>.<\/p>\n<div id=\"VietAd\">\n<h2>\n\t\t\tjQuery Plugin Design Pattern `A`<\/h2>\n<p>\n\t\t\tTi\u1ebfp theo, ch&uacute;ng ta s\u1ebd tham kh\u1ea3o v\u1ec1 ph\u1ea7n m&atilde; internal c\u1ee7a plugin t&ugrave;y &yacute; v\u1edbi <strong>function anonymous<\/strong> <strong>self-referencing<\/strong> v&agrave; ch\u1ee9c n\u0103ng l\u1ea5y c&aacute;c \u0111\u1ed1i t\u01b0\u1ee3ng jQuery d\u01b0\u1edbi d\u1ea1ng tham s\u1ed1. Nh\u01b0ng t\u1ea1i \u0111&acirc;y, ch&uacute;ng ta s\u1ebd d\u1ec5 b\u1ecb nh\u1ea7m l\u1eabn v\u1edbi t\u1eeb kh&oacute;a this \u0111\u01b0\u1ee3c tham chi\u1ebfu t\u1edbi trong ph\u1ea7n pattern n&agrave;y v&agrave; c&aacute;c v\u1ecb tr&iacute; kh&aacute;c trong to&agrave;n b\u1ed9 code. C\u1ee5 th\u1ec3, ch&uacute;ng t&ocirc;i \u0111&atilde; t\u1ea1o m\u1ed9t s\u1ed1 bi\u1ebfn local v\u1edbi t&ecirc;n l&agrave; vari &ndash; \u0111\u01b0\u1ee3c l\u01b0u tr\u1eef trong \u0111\u1ed1i t\u01b0\u1ee3ng jQuery ch&iacute;nh ($.vari) v&agrave; \u0111\u1ed1i t\u01b0\u1ee3ng <strong>jQuery fn ($.fn.vari)<\/strong>. Ch&uacute;ng c\u0169ng c&oacute; th\u1ec3 l&agrave; <strong>object function<\/strong>, nh\u01b0ng \u0111\u1ec3 \u0111\u01a1n gi\u1ea3n v&agrave; d\u1ec5 hi\u1ec3u th&igrave; ch&uacute;ng ta ch\u1ec9 s\u1eed d\u1ee5ng ph\u1ea7n bi\u1ebfn c\u01a1 b\u1ea3n m&agrave; th&ocirc;i. C&aacute;c b\u1ea1n s\u1ebd d\u1ec5 d&agrave;ng th\u1ea5y nh\u1eefng gi&aacute; tr\u1ecb n&agrave;y hi\u1ec3n th\u1ecb c&ugrave;ng v\u1edbi ch\u1ee9c n\u0103ng c\u1ea3nh b&aacute;o &ndash; <strong>alert<\/strong> trong \u0111o\u1ea1n m&atilde; d\u01b0\u1edbi \u0111&acirc;y.<\/p>\n<p>\n\t\t\tTr\u01b0\u1edbc ti&ecirc;n l&agrave; ph\u1ea7n pattern ch\u01b0a r&uacute;t g\u1ecdn:<\/p>\n<div style=\"margin: 10px auto;overflow:auto;background-\ncolor:#f7f8f8;width:600px;padding:10px; border:1px dashed\n#2f6fab;line-height:1.1em;  font-family:Courier New; font-\nstyle:italic;\"><\/p>\n<pre>\r\n<code>(function($)\r\n{\r\n    $.vari = &quot;$.vari&quot;;\r\n    $.fn.vari = &quot;$.fn.vari&quot;;<\/code><\/pre>\n<p>\n\t\t\t\t\/\/ $.fn is the object we add our custom functions to $.fn.DoSomethingLocal = function() { return this.each(function() { alert(this.vari); \/\/ would output `undefined` alert($(this).vari); \/\/ would output `$.fn.vari` }); }; })(jQuery);<\/p>\n<p>\n\t\t\t\t\/\/ $ is the main jQuery object, we can attach a global function to it $.DoSomethingGlobal = function() { alert(&quot;Do Something Globally, where `this.vari` = &quot; + this.vari); };<\/p>\n<p>\n\t\t\t\t<code>$(document).ready(function() { $(&quot;div&quot;).DoSomethingLocal(); $.DoSomethingGlobal(); });<\/code><\/p>\n<\/p><\/div>\n<p>\n\t\t\tV&agrave; ph\u1ea7n chi ti\u1ebft c\u1ee5 th\u1ec3:&nbsp;<\/p>\n<div style=\"margin: 10px auto;overflow:auto;background-\ncolor:#f7f8f8;width:600px;padding:10px; border:1px dashed\n#2f6fab;line-height:1.1em;  font-family:Courier New; font-\nstyle:italic;\"><\/p>\n<pre>\r\n<code>\/\/ plugin-name.js - define your plugin implementation pattern\r\n(function($) \/\/ The $ here signifies a parameter name\r\n             \/\/ As you can see from below, (jQuery) is\r\n             \/\/ immediately passed as the $ param\r\n{\r\n    $.vari = &quot;$.vari&quot;;\r\n    $.fn.vari = &quot;$.fn.vari&quot;;<\/code><\/pre>\n<p>\n\t\t\t\t\/\/ 1.) Add a custom interface `DoSomethingLocal` \/\/ Which will modify all selected elements! \/\/ If you are a software engineer, think about this as \/\/ a member function of the main jQuery class $.fn.DoSomethingLocal = function() { \/\/ return the object back to the chained call flow return this.each(function() \/\/ This is the main processor \/\/ function that executes on \/\/ each selected element \/\/ (e.g: jQuery(&quot;div&quot;)) { \/\/ this ~ refers to a DOM element \/\/ $(this) ~ refers to a jQuery object<\/p>\n<p>\n\t\t\t\t\/\/ Here, the `this` keyword is a self-refence to the \/\/ selected object `this.vari` is `undefined` because \/\/ it refers to selected DOM elements. So, we can do \/\/ something like: var borderStyle = this.style.border; \/\/ While $(this).vari, or jQuery(this).vari refers \/\/ to `$.fn.vari`<\/p>\n<p>\n\t\t\t\t\/\/ You would use the $(this) object to perform \/\/ any desired modification to the selected elements \/\/ $(this) is simply a reference to the jQuery object \/\/ of the selected elements alert(this.vari); \/\/ would output `undefined` alert($(this).vari); \/\/ would output `$.fn.vari` }); }; })(jQuery); \/\/ pass the jQuery object to this function<\/p>\n<p>\n\t\t\t\t\/\/ 2.) Or we can add a custom interface to the global jQuery \/\/ object. In this case, it makes no sense to enumerate \/\/ through objects with `each` keyword because this function \/\/ will theoretically work in the `global` scope. If you are \/\/ a professional software engineer, think about this \/\/ as a [static function] $.DoSomethingGlobal = function() { \/\/ this will output this.vari = $.vari alert(&quot;Do Something Globally, where `this.vari` = &quot; + this.vari); };<\/p>\n<p>\n\t\t\t\t<code>\/\/ index.html - test the plugin $(document).ready(function() { $(&quot;div&quot;).DoSomethingLocal(); $.DoSomethingGlobal(); });<\/code><\/p>\n<\/p><\/div>\n<p>\n\t\t\tTh\u1ef1c ra, c&oacute; 2 d\u1ea1ng giao di\u1ec7n kh&aacute;c nhau m&agrave; ch&uacute;ng ta c&oacute; th\u1ec3 g&aacute;n v&agrave;o object jQuery ch&iacute;nh \u0111&atilde; \u0111\u01b0\u1ee3c kh\u1edfi t\u1ea1o s\u1eb5n b\u1edfi framework. C&aacute;c b\u1ea1n h&atilde;y tham kh\u1ea3o k\u1ef9 v&iacute; d\u1ee5 tr&ecirc;n, t\u1ea1i nh\u1eefng n\u01a1i ch&uacute;ng t&ocirc;i \u0111&atilde; g&aacute;n th&ecirc;m 2 <strong>function<\/strong>, \u0111&oacute; l&agrave; <strong>DoSomethingLocal <\/strong>v&agrave; <strong>DoSomethingGlobal<\/strong>.<\/p>\n<ul>\n<li>\n\t\t\t\t<strong>DoSomethingLocal <\/strong>\u0111\u01b0\u1ee3c \u0111\u1ecbnh ngh\u0129a th&agrave;nh <strong>$.fn.DoSomethingLocal<\/strong>. Ng\u01b0\u1eddi d&ugrave;ng c&oacute; th\u1ec3 g&aacute;n nhi\u1ec1n function t&ugrave;y ch\u1ec9nh t\u1edbi \u0111\u1ed1i t\u01b0\u1ee3ng jQuery.fn (ho\u1eb7c $.fn) \u0111\u01b0\u1ee3c y&ecirc;u c\u1ea7u b\u1edfi qu&aacute; tr&igrave;nh th\u1ef1c hi\u1ec7n plugin. B\u1ea5t k\u1ef3 function n&agrave;o \u0111\u01b0\u1ee3c g&aacute;n t\u1edbi \u0111\u1ed1i t\u01b0\u1ee3ng $.fn \u0111\u1ec1u ho\u1ea1t \u0111\u1ed9ng tr&ecirc;n ph\u1ea7n t&ugrave;y ch\u1ec9nh c\u1ee7a 1 \u0111\u1ed1i t\u01b0\u1ee3ng ho\u1eb7c th&agrave;nh ph\u1ea7n n&agrave;o \u0111&oacute; \u0111\u01b0\u1ee3c l\u1ef1a ch\u1ecdn. V&agrave; \u0111&acirc;y c\u0169ng ch&iacute;nh l&agrave; &yacute; ngh\u0129a c\u1ee7a c&uacute; ph&aacute;p $.fn.<\/li>\n<li>\n\t\t\t\t<strong>DoSomethingGlobal <\/strong>\u0111\u01b0\u1ee3c &aacute;p d\u1ee5ng tr\u1ef1c ti\u1ebfp v&agrave;o \u0111\u1ed1i t\u01b0\u1ee3ng global jQuery d\u01b0\u1edbi d\u1ea1ng <strong>$.DoSomethingGlobal<\/strong>. 1 function \u0111\u01b0\u1ee3c g&aacute;n t\u1edbi framework jQuery theo c&aacute;ch nh\u01b0 v\u1eady s\u1ebd kh&ocirc;ng ho\u1ea1t \u0111\u1ed9ng tr&ecirc;n th&agrave;nh ph\u1ea7n \u0111\u01b0\u1ee3c l\u1ef1a ch\u1ecdn, nh\u01b0ng l\u1ea1i c&oacute; th\u1ec3 ho\u1ea1t \u0111\u1ed9ng trong global scope v&agrave; c&oacute; ch\u1ee9a b\u1ea5t k\u1ef3 kh\u1ea3 n\u0103ng th\u1ef1c hi\u1ec7n n&agrave;o.<\/li>\n<\/ul>\n<h2>\n\t\t\tjQuery Plugin Design Pattern `B`<\/h2>\n<p>\n\t\t\tTr&ecirc;n th\u1ef1c t\u1ebf th&igrave; nhi\u1ec1u ng\u01b0\u1eddi l\u1ea1i th&iacute;ch \u0111\u1eb7t \u0111o\u1ea1n m&atilde; c\u1ee7a plugin trong <strong>function anonymous<\/strong>, v&agrave; g\u1ecdi ra b\u1eb1ng c&aacute;ch g\u1eafn th&ecirc;m d\u1ea5u ngo\u1eb7c \u0111\u01a1n \u1edf ph&iacute;a cu\u1ed1i m\u1ec7nh \u0111\u1ec1, sau \u0111&oacute; chuy\u1ec3n \u0111\u1ed1i t\u01b0\u1ee3ng jQuery \u0111&oacute; t\u1edbi gi\u1ed1ng nh\u01b0 ph\u1ea7n <strong>jQuery Plugin Design Pattern `A`<\/strong> tr&ecirc;n, nh\u01b0ng li\u1ec7u ch&uacute;ng ta c&oacute; th\u1ef1c s\u1ef1 c\u1ea7n ph\u1ea3i l&agrave;m nh\u01b0 v\u1eady? Kh&ocirc;ng. H&atilde;y tham kh\u1ea3o 1 c&aacute;ch kh&aacute;c \u0111\u1ec3 t\u1ea1o <strong>plugin jQuery<\/strong> v\u1edbi c\u1ea5u tr&uacute;c \u0111\u01a1n gi\u1ea3n v&agrave; d\u1ec5 d&agrave;ng h\u01a1n.<\/p>\n<p>\n\t\t\tPh\u1ea7n pattern d\u01b0\u1edbi \u0111&acirc;y ph\u1ea3i \u0111\u01b0\u1ee3c s\u1eed d\u1ee5ng trong tr\u01b0\u1eddng h\u1ee3p ch&uacute;ng ta kh&ocirc;ng th\u1ef1c hi\u1ec7n ch\u1ee9c n\u0103ng <strong>chainability<\/strong>, ho\u1eb7c n&oacute;i theo c&aacute;ch kh&aacute;c l&agrave; <strong>function <\/strong>c\u1ee7a <strong>plugin <\/strong>s\u1ebd kh&ocirc;ng tr\u1ea3 l\u1ea1i \u0111\u1ed1i t\u01b0\u1ee3ng jQuery.<\/p>\n<div style=\"margin: 10px auto;overflow:auto;background-\ncolor:#f7f8f8;width:600px;padding:10px; border:1px dashed\n#2f6fab;line-height:1.1em;  font-family:Courier New; font-\nstyle:italic;\"><\/p>\n<pre>\r\n<code>\/\/ Plugin base object\r\n$.gShuffle = function()\r\n{<\/code><\/pre>\n<p>\n\t\t\t\t}<\/p>\n<p>\n\t\t\t\t\/\/ Initializes plugin $.gShuffle.initialize = function() {<\/p>\n<p>\n\t\t\t\t}<\/p>\n<p>\n\t\t\t\t\/\/ Runs the plugin $.gShuffle.run = function() {<\/p>\n<p>\n\t\t\t\t<code>};<\/code><\/p>\n<\/p><\/div>\n<p>\n\t\t\tHy v\u1ecdng r\u1eb1ng nh\u1eefng th&ocirc;ng tin tr&ecirc;n s\u1ebd gi&uacute;p b\u1ea1n hi\u1ec3u h\u01a1n v\u1ec1 c&aacute;c b\u01b0\u1edbc c\u01a1 b\u1ea3n t\u1ea1o plugin <strong>jQuery<\/strong>. Ch&uacute;c c&aacute;c b\u1ea1n th&agrave;nh c&ocirc;ng!<\/p>\n<\/p><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\n\t<span style=\"color: rgb(51, 51, 51);\">Trong b&agrave;i vi\u1ebft d\u01b0\u1edbi \u0111&acirc;y, ch&uacute;ng t&ocirc;i s\u1ebd gi\u1edbi thi\u1ec7u v&agrave; h\u01b0\u1edbng d\u1eabn c&aacute;c b\u1ea1n m\u1ed9t v&agrave;i b\u01b0\u1edbc c\u01a1 b\u1ea3n \u0111\u1ec3 t\u1ea1o plugin b\u1eb1ng<strong> jQuery<\/strong><\/span>. Cho d&ugrave; b\u1ea1n l&agrave; ng\u01b0\u1eddi m\u1edbi b\u1eaft \u0111\u1ea7u t&igrave;m hi\u1ec3u v\u1ec1 l\u0129nh v\u1ef1c ph&aacute;t tri\u1ec3n web ho\u1eb7c \u0111&atilde; c&oacute; th\u1eddi gian d&agrave;i ti\u1ebfp x&uacute;c v\u1edbi <strong>JavaScript<\/strong> th&igrave; qu\u1ea3 th\u1ef1c jQuery l&agrave; 1 n\u1ec1n t\u1ea3ng framework tuy\u1ec7t v\u1eddi v&agrave; kh&ocirc;ng th\u1ec3 b\u1ecf qua, \u0111\u1eb7c bi\u1ec7t l&agrave; nh\u1eefng ng\u01b0\u1eddi \u0111\u1ebfn v\u1edbi jQuery t\u1eeb n\u1ec1n t\u1ea3ng Javascript c&oacute; s\u1eb5n.<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[142],"tags":[],"class_list":["post-971","post","type-post","status-publish","format-standard","hentry","category-thu-thuat-jquery"],"views":444,"_links":{"self":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts\/971","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/comments?post=971"}],"version-history":[{"count":0,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts\/971\/revisions"}],"wp:attachment":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/media?parent=971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/categories?post=971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/tags?post=971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}