Mass framework uses many methods to generate an approximate method together, greatly reducingCodeTo improve maintainability.
For example, append, prepend, before, and after methods, in jquery2.0, they look like this:
Append: function () {return this. dommanip (arguments, true, function (ELEM) {If (this. nodetype = 1 | this. nodetype = 11 | this. nodetype = 9) {This. appendchild (ELEM) ;}}) ;}, prepend: function () {return this. dommanip (arguments, true, function (ELEM) {If (this. nodetype = 1 | this. nodetype = 11 | this. nodetype = 9) {This. insertbefore (ELEM, this. firstchild) ;}}) ;}, before: function () {return this. dommanip (arguments, false, function (ELEM) {If (this. parentnode) {This. parentnode. insertbefore (ELEM, this) ;}}, after: function () {return this. dommanip (arguments, false, function (ELEM) {If (this. parentnode) {This. parentnode. insertbefore (ELEM, this. nextsibling );}});},
The mass framework is implemented in this way, and the reverse method is also implemented.
// Append and replace "APPEND, prepend, before, after, replace ". replace ($. rword, function (method) {$. FN [Method] = function (item) {return manipulate (this, method, item, this. ownerdocument) ;}; $. FN [method + "to"] = function (item) {$ (item, this. ownerdocument) [Method] (this); return this ;};});
Jquery has three methods to remove nodes. The 2.0 implementation is as follows:
// Keepdata is for internal use only -- Do not documentremove: function (selector, keepdata) {var ELEM, I = 0, L = This. length; For (; I 0) {If (! Keepdata & ELEM. nodetype = 1) {jquery. cleandata (getall (ELEM);} If (ELEM. parentnode) {If (keepdata & jquery. contains (ELEM. ownerdocument, ELEM) {setglobaleval (getall (ELEM, "script");} ELEM. parentnode. removechild (ELEM) ;}}return this ;}, empty: function () {var ELEM, I = 0, L = This. length; For (; I
The mass Framework puts them together.
"Remove, empty, detach". Replace ($. rword, function (method) {$. FN [Method] = function () {var isremove = method! = "Empty"; for (VAR I = 0, node; node = This [I ++];) {If (node. nodetype = 1) {// remove the Matching Element var array = $. slice (node [tags] ("*"). concat (isremove? Node: []); If (method! = "Detach") {array. foreach (cleannode) ;}}if (isremove) {If (node. parentnode) {node. parentnode. removechild (node) ;}} else {While (node. firstchild) {node. removechild (node. firstchild) ;}}return this ;}});Cyclic generation is an art. It is necessary to have a deep understanding of their functions and commonalities, and then combine the special points into an object so that the if else in the method is minimized.
In addition, the dynamic parsing script is also a good way to look at the old Code of mass framework:
// Fade in $. FN. fadein = function (duration, complete) {var opts = {Duration: duration, complete: complete, inclutname: "fadein", classrule :". # {classname} {\ n \#{ prefix} animation-duration :#{ duration };\#{ prefix} animation-name :#{ framename }; \ # {prefix} animation-fill-mode: forwards; \} ", framerule:" # {prefix} keyframes # {framename} {\ to {\ opacity: 1; \}\} ", after: function (node) {node. style. opacity = 1 ;}return makeeffect (this, opts);} // fade out $. FN. fadeout = function (duration, complete) {var opts = {Duration: duration, complete: complete, inclutname: "fadeout", classrule :". # {classname} {\ n \#{ prefix} animation-duration :#{ duration };\#{ prefix} animation-name :#{ framename }; \ # {prefix} animation-fill-mode: forwards; \} ", framerule:" # {prefix} keyframes # {framename} {\ to {\ opacity: 0; \}\} ", after: function (node) {node. style. opacity = 0 ;}return makeeffect (this, opts );}The two methods are only different in the source code, so I changed them to the following:
$. FN. fadein = function (duration, complete) {var opts = {Duration: duration, complete: complete, inclutname: "fadein", classrule :". # {classname} {\ n \#{ prefix} animation-duration :#{ duration };\#{ prefix} animation-name :#{ framename }; \ # {prefix} animation-fill-mode: forwards; \} ", framerule:" # {prefix} keyframes # {framename} {\ to {\ opacity: 1; \}\} ", after: function (node) {node. style. opacity = 1 ;}return makeeffect (this, opts);} // fade out $. FN. fadeout = eval ("0," + $. FN. fadein. tostring (). replace ("1;", "0 ;"). replace ("facein", "fadeout "))