// Èç¹û²»Óù³×ÓµÄÇé¿ö // ¿¼Éú·ÖÊýÒÔ¼°¸¸Ç×Ãû function examinee(name, score, fatherName) { return { name: name, score: score, fatherName: fatherName }; } // ÉóÔÄ¿¼ÉúÃÇ function judge(examinees) { var result = {}; for (var i in examinees) { var curExaminee = examinees[i]; var ret = curExaminee.score; // ÅжÏÊÇ·ñÓкóÃŹØϵ if (curExaminee.fatherName === 'xijingping') { ret += 1000; } else if (curExaminee.fatherName === 'ligang') { ret += 100; } else if (curExaminee.fatherName === 'pengdehuai') { ret += 50; } result[curExaminee.name] = ret; } return result; } var lihao = examinee("lihao", 10, 'ligang'); var xida = examinee('xida', 8, 'xijinping'); var peng = examinee('peng', 60, 'pengdehuai'); var liaoxiaofeng = examinee('liaoxiaofeng', 100, 'liaodaniu'); var result = judge([lihao, xida, peng, liaoxiaofeng]); // ¸ù¾Ý·ÖÊýÑ¡È¡Ç°ÈýÃû for (var name in result) { console.log("name:" + name); console.log("score:" + score); }
¿ÉÒÔ¿´µ½£¬ÔÚÖмäÉóÔÄ¿¼ÉúÕâ¸öº¯ÊýÖУ¬ÔËÓÃÁ˺ܶà else if À´ÅжÏÊÇ·ñ¿¼ÉúÓкóÃŹØϵ£¬Èç¹ûÏÖÔÚÒµÎñ³¡¾°·¢Éú±ä»¯£¬ÓÖ¶àÁ˼¸Ãû¿¼Éú£¬ÄÇô else if ÊƱØÔ½À´Ô½¸´ÔÓ£¬Íùºóά»¤´úÂëÒ²½«Ô½À´Ô½Âé·³£¬³É±¾ºÜ´ó£¬ÄÇôÕâ¸öʱºòÈç¹ûʹÓù³×Ó»úÖÆ£¬¸ÃÈçºÎ×öÄØ£¿
// relationHook ÊǸö¹³×Óº¯Êý£¬ÓÃÓڵõ½¹ØϵµÃ·Ö var relationHook = { "xijinping": 1000, "ligang": 100, "pengdehuai": 50, ¡¡¡¡ // еĿ¼ÉúÖ»ÐèÒªÔÚ¹³×ÓÀïÌí¼Ó¹Øϵ·Ö } // ¿¼Éú·ÖÊýÒÔ¼°¸¸Ç×Ãû function examinee(name, score, fatherName) { return { name: name, score: score, fatherName: fatherName }; } // ÉóÔÄ¿¼ÉúÃÇ function judge(examinees) { var result = {}; for (var i in examinees) { var curExaminee = examinees[i]; var ret = curExaminee.score; if (relationHook[curExaminee.fatherName] ) { ret += relationHook[curExaminee.fatherName] ; } result[curExaminee.name] = ret; } return result; } var lihao = examinee("lihao", 10, 'ligang'); var xida = examinee('xida', 8, 'xijinping'); var peng = examinee('peng', 60, 'pengdehuai'); var liaoxiaofeng = examinee('liaoxiaofeng', 100, 'liaodaniu'); var result = judge([lihao, xida, peng, liaoxiaofeng]); // ¸ù¾Ý·ÖÊýÑ¡È¡Ç°ÈýÃû for (var name in result) { console.log("name:" + name); console.log("score:" + score); }
¿ÉÒÔ¿´µ½£¬Ê¹Óù³×ÓÈ¥´¦ÀíÌØÊâÇé¿ö£¬¿ÉÒÔÈôúÂëµÄÂß¼¸ü¼ÓÇåÎú£¬Ê¡È¥´óÁ¿µÄÌõ¼þÅжϣ¬ÉÏÃæµÄ¹³×Ó»úÖƵÄʵÏÖ·½Ê½£¬²ÉÓõľÍÊDZíÇý¶¯·½Ê½£¬¾ÍÊÇÎÒÃÇÊÂÏÈÔ¤¶¨ºÃÒ»ÕÅ±í£¨Ë׳ƴò±í£©£¬ÓÃÕâÕűíÈ¥ÊÊÅäÌØÊâÇé¿ö¡£µ±È» jQuery µÄ hook ÊÇÒ»ÖÖ¸üΪ³éÏóµÄ¸ÅÄÔÚ²»Í¬³¡¾°¿ÉÒÔÓò»Í¬·½Ê½ÊµÏÖ¡£
¿´¿´ jQuery ÀïµÄ±íÇý¶¯ hook ʵÏÖ£¬$.type ·½·¨£º
(function(window, undefined) { var // ÓÃÓÚÔ¤´æ´¢Ò»ÕÅÀàÐͱíÓÃÓÚ hook class2type = {}; // ÔÉúµÄ typeof ·½·¨²¢²»ÄÜÇø·Ö³öÒ»¸ö±äÁ¿ËüÊÇ Array ¡¢RegExp µÈ object ÀàÐÍ£¬jQuery ΪÁËÀ©Õ¹ typeof µÄ±í´ïÁ¦£¬Òò´ËÓÐÁË $.type ·½·¨ // Õë¶ÔһЩÌØÊâµÄ¶ÔÏó£¨ÀýÈç null£¬Array£¬RegExp£©Ò²½øÐо«×¼µÄÀàÐÍÅÐ¶Ï // ÔËÓÃÁ˹³×Ó»úÖÆ£¬ÅжÏÀàÐÍÇ°£¬½«³£¼ûÀàÐÍ´ò±í£¬ÏÈ´æÓÚÒ»¸ö Hash ±í class2type Àï±ß jQuery.each("Boolean Number String Function Array Date RegExp Object Error".split(" "), function(i, name) { class2type["[object " + name + "]"] = name.toLowerCase(); }); jQuery.extend({ // È·¶¨JavaScript ¶ÔÏóµÄÀàÐÍ // Õâ¸ö·½·¨µÄ¹Ø¼üÖ®´¦ÔÚÓÚ class2type[core_toString.call(obj)] // ¿ÉÒÔʹµÃ typeof obj Ϊ "object" ÀàÐ͵ĵõ½¸ü½øÒ»²½µÄ¾«È·ÅÐ¶Ï type: function(obj) { if (obj == null) { return String(obj); } // ÀûÓÃÊÂÏÈ´æºÃµÄ hash ±í class2type ×÷¾«×¼ÅÐ¶Ï // ÕâÀïÒòΪ hook µÄ´æÔÚ£¬Ê¡È¥ÁË´óÁ¿µÄ else if ÅÐ¶Ï return typeof obj === "object" || typeof obj === "function" ? class2type[core_toString.call(obj)] || "object" : typeof obj; } }) })(window);
¡¡