º¯ÊýÖØÔØ
ÔÚÆäËûÓïÑÔÖУ¬ÎÒÃÇÒ»°ã¶¼Ìý˵¹ýÖØÔصĸÅÄ¶ÔÓÚÒ»¸ö·½·¨£¬Èç¹ûÎÒÃÇÆÚÍû´«È벻ͬµÄ²ÎÊýÀàÐÍ£¬»òÕß´«È벻ͬµÄ²ÎÊý¸öÊý£¬ÉõÖÁ´«Èë²ÎÊýµÄ˳Ðò²»Ò»Ñù£¬¶øÈ¥Ö´Ðв»Í¬µÄ´¦Àí´úÂ룬ÒÔ·µ»ØÏàÓ¦µÄ½á¹û£¬ÄÇôËûÃǵķ½·¨ÃûÊÇ¿ÉÒÔÏàͬµÄ£¬¶øÇÒËüÃDz»»á²úÉú³åÍ»£¬Õâ¾ÍÊÇËùνµÄÖØÔØ¡£
ÕâÊÇÒòΪÏñjavaµÈÕâÑùµÄÇ¿ÀàÐÍÓïÑÔ£¬ËûÃǾßÓÐÀàÐÍÔ¼ÊøºÍ·½·¨Ç©Ãû£¬ÕâÑù¾ÍʹµÃËûÃÇÄܹ»¸ù¾Ýµ÷ÓÃʱ´«Èë²ÎÊýµÄÇé¿öÀ´¾ö¶¨Ê¹ÓÃÄÄÒ»¸öͬÃû·½·¨À´´¦ÀíÐèÒªµÄ²Ù×÷¡£
ÊÇÔÚ˵ÎÒÂð£¿
µ«ÊÇjsÊôÓÚÈõÀàÐÍÓïÑÔ£¬Ò²¾ÍÊÇ˵Ëü²»»á¹æ¶¨²ÎÊý±ØÐë´«ÈëÄÄÖÖÀàÐÍ£¬¶¨ÒåµÄ·½·¨Ò²²»¾ßÓÐÇ©ÃûµÄ¹¦ÄÜ£¬¶øÇÒÔÚ¶¨Òå¶à¸öͬÃû·½·¨Ê±£¬ÀàËÆÓÚcssÀïÃæµÄ²ãµþÑùʽ±íµÄЧ¹û£¬ºó¶¨ÒåµÄͬÃû·½·¨»á¸²¸ÇÇ°ÃæµÄ·½·¨£¬»¹ÓÐÒ»¸ö¾ÍÊǺ¯Êý¾ßÓÐÌáÉýµÄÌØÐÔ£¬ÕâÒ²¾ÍʹµÃËüÎÞ·¨ÊµÏÖÖØÔصŦÄÜ¡£
Ôõô°ìÄØ£¿
ÆäʵjsÖÐҲȷʵ²»ÐèÒªÖØÔصŦÄÜ£¬ÒòΪûÓÐÀàÐ͵ÄÔ¼Êø£¬ÔÚÒ»¸ö·½·¨ÀïÃæ¾Í¿ÉÒÔ×öºÜ¶à×ÔÓɵķ¢»Ó£¬²»µ«ÄÜÂú×ãÐèÒªÖØÔصÄÐèÇ󣬶øÇÒ»¹ÄÜÍæһЩ»¨Ñù¡£
²»¹ý»°ÓÖ˵»ØÀ´£¬Ã»ÓÐÁËÔ¼Êø£¬¾ÍÈÝÒ×·¸´í£¬¶¼ÓÃÒ»¸ö·½·¨ÌåÀ´´¦ÀíËùÓÐÇé¿ö£¬¾Í»áÈÝÒ׳öÂÒ×Ó£¬Ê¹µÃÎÒÃÇÐèҪʹÓÃÒ»¶ÑµÄÀàËÆif-elseµÄÓï¾äÀ´×öµ½ÕâÒ»µã¡£
ÄÇôÎÒÃÇÄܲ»ÄÜÔÚÏÖÓÐjsÔËÐз½Ê½µÄ»ù´¡ÉÏ£¬½è¼øÆäËûÓïÑÔ¶ÔÓÚÖØÔصÄÔËÓã¬À´ÈƸöÍä×Ó±äÁ¿À´ÊµÏÖÒ»ÏÂÊôÓÚjs×Ô¼ºµÄÖØÔØ·½Ê½ÄØ£¿
ÊÔÒ»ÊÔ
ÎÒÃǽñÌìÖ»ÌÖÂÛÔÚjsÖбäÏàʵÏÖÖØÔصķ½Ê½£¬¶ø²»ÌÖÂÛËüµÄÒâÒåÓëʵ¼ÊÓ¦Óó¡¾°£¬ÎÒÃÇͨ¹ýÒ»¸ö¼òµ¥µÄÀý×Ó£¬À´Ö§³ÅÕâ¸öÌÖÂÛ£¬ÆäËûµÄ½»ÓÉÄãÃÇÀ´×ÔÓÉ·¢»Ó¡£
ÈÃÎÒÃÇ¿ªÊ¼°É
jsÖØÔصķ½Ê½ÔÚjsÖÐÖ÷ÒªÓÐÒÔϼ¸ÖÖʵÏÖÖØÔصķ½Ê½£º
ʹÓÃÊ£Óà²ÎÊýµÄÐÎʽÀ´½ÓÊտɱäµÄ²ÎÊý£¬²¢Í¨¹ýһЩÅжÏÊÖ¶ÎÀ´´¦Àí²»Í¬Çé¿öµÄÂß¼¡£Ê¹ÓÃargumentsµÄÐÎʽ£¬À´¶¯Ì¬ÅжÏÐèÒªÖ´ÐеIJÙ×÷ʹÓÃproxyµÄÐÎʽÀ´À¹½Øº¯ÊýµÄÐÐΪ£¬ÒÔ´ïµ½¿ØÖƲ»Í¬µÄÂß¼·ÖÖ§¡£Ç°Á½ÖÖ·½Ê½±È½ÏÏàËÆ£¬Ë¼Â·Ò»Ñù£¬Ö»ÊÇʵÏÖÊÖ¶ÎÓÐËù²»Í¬£¬Óû§ÐèÒª×Ô¼ºÐ´ÅжÏÂß¼¡£µÚÈýÖÖ·½Ê½½áºÏproxyÀ´Òþ²ØʵÏÖϸ½Ú£¬ÈÃÓû§Ö»¹Ø×¢¸÷×Եķֹ¤¡£µ«ËüÃǶ¼ÊÇÔÚ²ÎÊý¶¯Ì¬ÅжϷ½Ãæ×öÎÄÕ¡£
Ç°Á½ÖÖ·½Ê½µÄÓÅȱµã£º
Óŵ㣺¿ÉÒÔÖ±½Ó¶¨Ò庯Êý»òʹÓñí´ïʽ
ȱµã£ºº¯ÊýÃû²»ÄÜÏàͬ£¬ÐèҪд½Ï¶àµÄÅжϷÖÖ§
µÚÈýÖÖ·½Ê½µÄÓÅȱµã£º
Óŵ㣺¿ÉÒÔ²»ÓÃд¸÷ÖÖ²ÎÊýÐÎʽµÄ·ÖÖ§
ȱµã£ºÖ»ÄÜʹÓñí´ïʽ¶¨Ò庯Êý
ÓÉÓÚÇ°Á½ÖÖÍøÉÏÒѾÓкܶàµÄʵÏÖ˼·Óë·½°¸£¬Òò´ËÕâÀï²»ÔÙ½øÐÐ̽ÌÖ£¬ÆäÖÐÓкܶàÆæÃîµÄʵÏÖ£¬¿ÉÒÔ×öµ½ÔÚjsÖÐʹÓÃÖØÔصÄ˼Ïë¡£
ËùÒÔÔÚ´ËÎÒÃÇÖ»ÌÖÂÛµÚÈýÖÖ·½°¸£¬ÎÒÃÇÏÂÃæÀ´¿´Ò»ÏÂËüµÄ˼·ÊÇʲô£¬ÊÇ·ñÂú×ãÖØÔصÄÐèÇó£¬ËüÊÇÈçºÎʵÏֵģ¬ÒÔ¼°ËüÄÜÂú×ãÎÒÃÇʲôÑùµÄÐèÇó£¿
ÕâÊÇʲôÄØ£¿
ÐèÇó¼ÙÉèÎÒÃÇÏÖÔÚÓÐÕâÑùÒ»¸ö³¡¾°ºÍÐèÇó£º
×Ô¼º¿ªÁËÒ»¼Ò·þ×°µê£¬ÓÉÓÚÉúÒâ»ð±¬£¬ÎÒÃÇÏëÒª´ðлÐÂÀϹ˿ͣ¬ÏÖÔÚÍƳöÁËÒ»¸ö»î¶¯£¬È«³¡ÎÞÂÛÈκηþ×°£¬Ö»ÒªÂòÒ»¼þ¾ÍÖ±½Ó´ò95ÕÛ£¬Ö»ÒªÂòÁ½¼þ¾ÍÈ«²¿´ò9ÕÛ£¬Ö»ÒªÂòÈý¼þ¾ÍÈ«²¿´ò85ÕÛ£¬Ö»ÒªÂòËļþ¼°ÒÔÉÏ£¬¾ÍÈ«²¿´ò8ÕÛ¡£
Èç¹ûÓôúÂëÀ´ÊµÏÖ£¬Æäʵ¾ÍÊǸø·½·¨Öд«ÈëÒ»¸öÁ½¸öÈý¸öËĸö²ÎÊýµÄÎÊÌ⣬Òò´ËÎÒÃÇ×ÔÈ»¶øÈ»µÄ¾ÍÏëµ½ÁËʹÓÃÖØÔØÀ´ÊµÏÖÕâ¸öÐèÇó¡£
½ÓÏÂÀ´ÎÒÃǾÍÊÔ×Å×Ô¼ºÊµÏÖÒ»¸öÕâÑùµÄ¹¦ÄÜ£¬¿´¿´¿É²»¿ÉÒÔ´´½¨Ò»¸ö¸³ÄÜ·½·¨À´Ê¹Ä³¸öÒµÎñ´¦Àíº¯Êý¾ßÓÐÖØÔصÄÄÜÁ¦¡£
˼··ÖÎöÒªÉú³ÉÕâÑùÒ»¸ö¸³ÄÜ·½·¨£¬ÎÒÃÇÐèÒªÓжԺ¯Êý¸ÄÔìµÄÄÜÁ¦£¬ÔÚ´´½¨ÒµÎñ´¦Àíº¯ÊýµÄʱºò£¬×îºÃÄܹ»¸Ä±äËüµÄĬÈÏÐÐΪ£¬ÔÚÖ´ÐеÄʱºòÒ²Äܹ»¶ÔËü½øÐÐÀ¹½ØÒÔ×öһЩ¶îÍâµÄ²Ù×÷¡£
ÄÇôÎÒÃǺÜ×ÔÈ»¶øÈ»µÄ¾ÍÏëµ½ÁËʹÓÃProxy£¬ÏÈÉú³ÉÒ»¸öProxyº¯Êý£¬È»ºóÔÚ¸øËüÉèÖÃÊôÐÔµÄʱºò£¬ÎÒÃǽøÐÐÀ¹½Ø£¬°Ñ¸³Öµ²Ù×÷ÖеÄvalue»º´æÆðÀ´£¬ÒÔ±¸½«À´µ÷ÓõÄʱºò×ö·ÖÖ§´¦Àí£¬¸ù¾Ý²ÎÊýµÄ¸öÊýÓëÀàÐÍÀ´¿ØÖÆÐèÒªÖ´ÐеÄÒµÎñÂß¼´úÂë¡£ËüÕæµÄÄÜ×öµ½Âð£¿ÎÒÃÇÀ´¿´Ò»ÏÂÏÂÃæµÄÒ»²½²½´úÂëʵÏÖ¡£
ʵÏÖÐèÇóFunction Overload(defaultCall) { let func = defaultCall || new Function() func.overloadCached = [] return new Proxy(func, { set(target, prop, value) { if(prop === 'load') { target.overloadCached.push(value) } }, apply(target, thisArg, argumentsList) { for(let i = target.OverloadCached.length - 1; i > -1; i--) { if(argumentsList.length === target.overloadCached[i].length || (argumentsList.length > target.overloadCached[i].length)) { return target.overloadCached[i].apply(thisArg, argumentsList) } } return target.apply(thisArg, argumentsList) } })}let sum = Overload()sum.load = function (a) { return a * 0.95;}sum.load = function (a, b) { return (a b) * 0.9;}sum.load = function (a, b, c) { return (a b c) * 0.85;}sum.load = function (a, b, c, d, ...arg) { return (arg.concat(a,b,c,d).reduce((total, cur) => {return total cur},0)) * 0.8;}console.log(sum(200));console.log(sum(200, 300));console.log(sum(180, 280, 190));console.log(sum(270, 260, 310, 240));console.log(sum(180, 220, 240, 210, 190));//Êä³ö£º190,450,552.5,864,832¿ÉÒÔ¿´µ½£¬ÎÒÃÇʵÏÖÁËÒ»¸öOverloadº¯Êý£¬ÓÃÀ´·µ»ØÒ»¸öProxy£¬Í¨¹ýËüÈ¥load²»Í¬µÄ·½·¨À´ÊµÏÖ¶ÔͬÃû·½·¨µÄÖØÔØ£¬µ÷ÓõÄʱºòÖ»ÐèÒªÒ»¸ö·½·¨Ãû¼´¿É£¬ProxyÖÐÎÒÃǶÔset(¼´ÉèÖøÃProxyµÄÖµµÄ²Ù×÷)ºÍapply(¼´Ö´ÐиÃProxyµÄ²Ù×÷)Á½ÖÖ²Ù×÷½øÐÐÁËÀ¹½Ø£¬Óõ½ÁËÒ»¸ö½Ð×öoverloadCachedµÄÊôÐÔÀ´»º´æÎÒÃǵĴ¦Àíº¯Êý£¬ÔÚµ÷Óú¯ÊýµÄʱºò£¬ÎÒÃÇʹÓôӺóÍùÇ°±éÀúµÄ·½Ê½£¬À´´ïµ½ºó¶¨ÒåÓÅÏÈÉúЧµÄÔÔò¡£
ͨ¹ý´òÓ¡½á¹ûÎÒÃÇÖªµÀ£¬ËüÒѾÂú×ãÁËÎÒÃǵÄÐèÇó¼ÙÉè¡£
ĬÈÏ´¦Àí´ÓÉÏÃæµÄ´úÂëÖÐÎÒÃÇ·¢ÏÖ£¬Overloadº¯Êý¿ÉÒÔ´«ÈëÒ»¸ö½Ð×ödefaultCallµÄ²ÎÊý£¬ËüÊÇÓÃÀ´´¦ÀíĬÈϲÙ×÷µÄ£¬Ò²¾ÍÊÇ˵Èç¹ûºóÃ涨ÒåµÄËùÓз½·¨¶¼²»Äܹ»´¦ÀíµÄʱºò£¬½«Ê¹ÓøÃĬÈϺ¯Êý½øÐд¦Àí£¬Èç¹ûûÓж¨Òå¸Ãº¯Êý£¬ÄÇôµ÷ÓÃsumʱÈç¹ûûÓÐÂú×ãµÄÖ´Ðк¯Êý£¬½«»á·µ»Øundefined¡£
ÏÖÔÚÎÒÃǸøËü´«ÈëÒ»¸öĬÈϵĴ¦Àíº¯Êý£¬ÄÇôÉÏÃæµÄÐèÇ󽫿ÉÒÔд³ÉÕâÑù£º
function Overload(defaultCall) { let func = defaultCall || new Function() func.overloadCached = [] return new Proxy(func, { set(target, prop, value) { if(prop === 'load') { target.overloadCached.push(value) } }, apply(target, thisArg, argumentsList) { for(let i = target.overloadCached.length - 1; i > -1; i--) { //×¢ÒâÕâÀïµÄ±ä»¯ if(argumentsList.length === target.overloadCached[i].length) { return target.overloadCached[i].apply(thisArg, argumentsList) } } return target.apply(thisArg, argumentsList) } })}let sum = Overload(function () { return ([].__proto__.reduce.call(arguments, (total, cur) => {return total cur},0)) * 0.8;})sum.load = function (a) { return a * 0.95;}sum.load = function (a, b) { return (a b) * 0.9;}sum.load = function (a, b, c) { return (a b c) * 0.85;}console.log(sum(200));console.log(sum(200, 300));console.log(sum(180, 280, 190));console.log(sum(270, 260, 310, 240));console.log(sum(180, 220, 240, 210, 190));//Êä³ö£º190,450,552.5,864,832ÎÒÃÇ×¢ÒâOverloadº¯ÊýµÄ±ä»¯£¬ÏÖÔÚÒÀÈ»Âú×ãÉÏÃæµÄÐèÇó¡£
´¦Àí¼æÈÝÓÉÓÚÎÒÃÇ°ÑËĸö²ÎÊý¼´ÒÔÉϵĴ¦Àíº¯Êý¸ÄΪͨ¹ý´«ÈëĬÈϺ¯ÊýµÄ·½Ê½À´ÊµÏÖ£¬Òò´ËÎÒÃÇÐÞ¸ÄÁËOverload·½·¨£¬ÕâÏÔÈ»ÊDz»ºÏÀíµÄ£¬ÒòΪÕâÑù²»ÉèÖÃĬÈϺ¯ÊýµÄʱºò»á³öÎÊÌ⣬Òò´ËÎÒÃÇ×öÒ»¸ö¼æÈÝ´¦Àí£¬ÐÞ¸ÄÖ®ºó¾Í±ä³ÉÁËÕâÑù£º
function Overload(defaultCall) { let func = defaultCall || new Function() func.overloadCached = [] return new Proxy(func, { set(target, prop, value) { if(prop === 'load') { let str = value.toString() let m1 = str.match(/(. ?)/) if(m1 && m1[0].indexOf("...") != -1) { value.rest = true } target.overloadCached.push(value) } }, apply(target, thisArg, argumentsList) { for(let i = target.overloadCached.length - 1; i > -1; i--) { if((argumentsList.length === target.overloadCached[i].length) || (target.overloadCached[i].rest && argumentsList.length > target.overloadCached[i].length)) { return target.overloadCached[i].apply(thisArg, argumentsList) } } return target.apply(thisArg, argumentsList) } })}//Êä³ö£º190,450,552.5,864,832ÏÖÔÚʹÓÃÕâ¸öOverloadº¯Êý¾ÍÒѾÄܹ»´¦ÀíÉÏÃæµÄÕâÁ½ÖÖÇé¿öÁË¡£ÎÒÃÇÉ趨ÁËÒ»¸örestÊôÐÔÀ´¸ø·½·¨´òÉÏÁËÒ»¸ö±êʶ¡£
ÐèÇóÑÓÉìÈç¹ûÎÒÃÇÏÖÔÚÔÚÉÏÃæµÄÐèÇó»ù´¡ÉÏ£¬ÓÖÏëÒª¶Ô½ð¶î×öһЩ´¦Àí£¬±ÈÈçÏ£ÍûÄܹ»¼ÓÉÏ$¡¢£¤¡¢€µÈǰ׺£¬À´Çø·Ö²»Í¬µÄ±ÒÖÖ¡£
Õâ¸öʱºòÎÒÃÇÐèÒªÔö¼ÓеÄÖØÔغ¯Êý£¬¶ø¼ÓÁ˱ÒÖÖÖ®ºóµÄº¯Êý¿ÉÄÜÓëÏÖÓеĺ¯Êý²ÎÊý¸öÊýÏàͬ(±ÈÈçsum('$', 220, 240)ºÍsum(270, 260, 310))£¬Õâ¾ÍÔì³ÉÁËÎóÅУ¬ÄÇôÎÒÃÇÄܲ»ÄÜÔÙ×öÒ»¸öÀàÐÍÇø·ÖÄØ£¿
Ó¦¸ÃÊÇ¿ÉÒԵģ¬µ«ÊÇÎÒÃDZØÐëÔ¼¶¨Ò»ÖÖ¸ñʽ£¬±ÈÈçÏÂÃæÕâÖÖÐÎʽ£¬ÎÒÃÇÐèÒªÔÚ»ñÈ¡ProxyÊôÐÔµÄʱºò(ÕâÀï¾ÍÓõ½ÁËÀ¹½Ø»ñÈ¡ProxyÊôÐԵIJÙ×÷)£¬½«ÀàÐͽøÐлº´æ£¬ÒԱ㽫À´Ê±ºòµÄʱºòÀ´×öÀàÐ͵ÄÅжϣº
//ÎÒÃÇÔ¼¶¨ÁË10ÖÖÀàÐÍ//n¡únumber//s¡ústring//b¡úBoolean//o¡úobject//a¡úarray//d¡údate//S¡úSymbol//r¡úregexp//B¡úbigint//f¡úfunctionfunction Overload(defaultCall) { let func = defaultCall || new Function() func.overloadCached = [] func.modifier = [] return new Proxy(func, { get(target, property, receiver) { if(property !== 'load') { target.modifier.push(property) } return receiver }, set(target, prop, value) { if(['n','s','b','o','a','d','S','r','B','f'].includes(prop)) { target.modifier.push(prop) } if(prop === 'load' || target.modifier.length !== 0) { let str = value.toString() let m1 = str.match(/(. ?)/) if(m1 && m1[0].indexOf("...") != -1) { value.rest = true } value.modifier = target.modifier target.overloadCached.push(value) target.modifier = [] } }, apply(target, thisArg, argumentsList) { for(let i = target.overloadCached.length - 1; i > -1; i--) { if((argumentsList.length === target.overloadCached[i].length) || (target.overloadCached[i].rest && argumentsList.length > target.overloadCached[i].length)) { if(target.overloadCached[i].modifier.length !== 0){ let ty = { '[object Number]': ['n'], '[object String]': ['s'], '[object Boolean]': ['b'], '[object Object]': ['o'], '[object Array]': ['a'], '[object Date]': ['d'], '[object Symbol]': ['S'], '[object Regexp]': ['r'], '[object BigInt]': ['B'], '[object Function]': ['f'], } if(target.overloadCached[i].modifier.some((m, j) => { return !ty[({}).__proto__.toString.call(argumentsList[j])].includes(m) })) { continue } } return target.overloadCached[i].apply(thisArg, argumentsList) } } return target.apply(thisArg, argumentsList) } })}let sum = Overload()sum.load.n = function (a) { return a * 0.95;}sum.load.n.n = function (a, b) { return (a b) * 0.9;}sum.load.n.n.n = function (a, b, c) { return (a b c) * 0.85;}sum.load.s.n.n = function (a, b, c) { return a (b c) * 0.85;}sum.load.n.n.n.n = function (a, b, c, d, ...arg) { return (arg.concat(a,b,c,d).reduce((total, cur) => {return total cur},0)) * 0.8;}sum.load.s.n.n.n = function (a, b, c, d, ...arg) { return a (arg.concat(b,c,d).reduce((total, cur) => {return total cur},0)) * 0.8;}console.log(sum(200));console.log(sum(200, 300));console.log(sum(260, 310, 240));console.log(sum('€', 280, 190));console.log(sum(180, 220, 240, 210, 190));console.log(sum('$', 220, 240, 210, 190));//Êä³ö£º190,450,688.5,€399.5,832,$688ÎÒÃÇÏÖÔÚÒѾ¼ÓÉÏÁËÀàÐÍÅжϣ¬Í¨¹ý´«ÈëµÄ²ÎÊýÀàÐÍÓë¸öÊýµÄ²»Í¬£¬Äܹ»ÏàÓ¦µÄÈ¥Ö´ÐжÔÓ¦µÄº¯Êý£¬Æäʵ²ÎÊýµÄ˳ÐòÒ»¸öµÀÀí£¬Ò²ÊÇÖ§³ÖµÄ¡£
ÀàÐÍÀ©Õ¹ÉÏÃæµÄÀàÐÍÔ¼¶¨ÎÒÃÇ¿ÉÄÜ¿´ÆðÀ´¹Ö¹ÖµÄ£¬¶øÇұȽÏÄÑÒÔÀí½â£¬Òò´ËÎÒÃÇ¿ÉÒÔÀ©Õ¹Ò»ÏÂÀàÐÍÔ¼¶¨µÄ±íʾ·½Ê½£¬¸ÄÔìºóµÄOverloadº¯ÊýÈçÏ£º
function Overload(defaultCall) { let func = defaultCall || new Function() func.overloadCached = [] func.modifier = [] return new Proxy(func, { get(target, property, receiver) { if(property !== 'load') { if(property.indexOf(',') !== -1) { property.split(',').map(item => { target.modifier.push(item) }) }else{ property.split('').map(item => { target.modifier.push(item) }) } } return receiver }, set(target, prop, value) { let modi = null if(prop.indexOf(',') !== -1) { modi = prop.split(',') }else{ modi = prop.split('') } if(modi.every(p => { return ['n','s','b','o','a','d','S','r','B','f','number','string','boolean','object','array','date','Symbol','regexp','bigint','function'].includes(p) })) { modi.map(item => { target.modifier.push(item) }) } if(prop === 'load' || target.modifier.length !== 0) { let str = value.toString() let m1 = str.match(/(. ?)/) if(m1 && m1[0].indexOf("...") != -1) { value.rest = true } value.modifier = target.modifier target.overloadCached.push(value) target.modifier = [] } }, apply(target, thisArg, argumentsList) { for(let i = target.overloadCached.length - 1; i > -1; i--) { if((argumentsList.length === target.overloadCached[i].length) || (target.overloadCached[i].rest && argumentsList.length > target.overloadCached[i].length)) { if(target.overloadCached[i].modifier.length !== 0){ let ty = { '[object Number]': ['n','number'], '[object String]': ['s','string'], '[object Boolean]': ['b','boolean'], '[object Object]': ['o','object'], '[object Array]': ['a','array'], '[object Date]': ['d','date'], '[object Symbol]': ['S','Symbol'], '[object Regexp]': ['r','regexp'], '[object BigInt]': ['B','bigint'], '[object Function]': ['f','function'], } if(target.overloadCached[i].modifier.some((m, j) => { return !ty[({}).__proto__.toString.call(argumentsList[j])].includes(m) })) { continue } } return target.overloadCached[i].apply(thisArg, argumentsList) } } return target.apply(thisArg, argumentsList) } })}ÕâÑùÎÒÃǾͿÉÒÔÖ§³Öһϼ¸ÖÖÀàÐÍÔ¼¶¨µÄÊéдÐÎʽ£º
sum.load.s.n.n = function (a, b, c) { return a (b c) * 0.85;}sum.load['snn'] = function (a, b, c) { return a (b c) * 0.85;}sum.load.snn = function (a, b, c) { return a (b c) * 0.85;}//¶ÔÓÚÈ«³Æ²»Äܹ»Ð´³É.(µã)µÄÐÎʽsum.load['string,number,number'] = function (a, b, c) { return a (b c) * 0.85;}//ÕâËÄÖÖÐÎʽµÄÈÎÒâÒ»ÖÖ¶ÔÓÚconsole.log(sum('$', 280, 190));//¶¼»áÊä³ö£º$399.5µ½´ËΪֹ£¬ÎÒÃÇÒѾÄܹ»Ö§³Ö²ÎÊýµÄ¸öÊý¡¢ÀàÐÍ¡¢Ë³ÐòµÄ²»Í¬£¬»áÖ´Ðв»Í¬µÄ´¦Àíº¯Êý£¬Âú×ãÁËÖØÔصĻù±¾ÐèÇó£¬Íê³ÉÁËÎÒÃÇÔÚ×ʼµÄÐèÇó¼ÙÉèµÄʵÏÖ¡£
½áÓïÄ¿Ç°ÕâÖÖ·½Ê½Ö»ÄÜÖ§³Öº¯Êý±í´ïʽµÄ·½Ê½À´½øÐÐÖØÔØ£¬ÕâÀïÖ»ÊǸø´ó¼ÒÌṩһ¸ö×Ô¶¨ÒåʵÏÖÖØÔصķ½Ê½£¬½áºÏ×Ô¼ºµÄÒµÎñ³¡¾°£¬Ð¡»ï°éÃÇ¿ÉÒÔ×ÔÓÉ·¢»Ó£¬ÆäʵĿǰjsµÄ¼ÈÓз½Ê½ÄÜÂú×ãÎÒÃÇÐèÒªÖØÔصij¡¾°£¬¶ø²»ÐèÒª¶îÍâÉè¼ÆÖØÔصĴúÂë¡£
¾ßÌåÕâÖÖ·½Ê½µÄÓÅÁÓ£¬´ó¼Ò¿ÉÒÔ×ÔÐÐÅжϣ¬²¢ÇÒ¿ÉÒÔ¸ù¾ÝÕâÖÖ˼·ÖØÐÂÉè¼ÆÒ»ÏÂʵÏÖµÄÊֶΡ£
лл
jsÖз½·¨ÖØÔØÈçºÎʵÏÖ£¿ÒÔ¼°º¯ÊýµÄ²ÎÊýÎÊÌâ
¶¼ÖªµÀÔÚjsÖÐûÓа취ֱ½ÓʵÏÖ·½·¨ÖØÔØ£¬ÒòΪÔÚjsÖÐÈç¹û¶¨ÒåÁ˶à¸öÃû³ÆÏàͬ£¬µ«²ÎÊý¸öÊý²»Ò»ÑùµÄ·½·¨£¬ÆäʵֻÓÐ×îºóÒ»¸ö·½·¨Äܱ»ÕæÕýµ÷Óã¬ÆäËûµÄ·½·¨¶¼±»¸²¸ÇµôÁË¡£ÈçºÎʵÏÖJSº¯ÊýµÄÖØÔØ
javascript²»ÄÜÖ§³Öº¯ÊýµÄÖØÔØ£¬ÈçÏ£º¸´ÖÆ´úÂë´úÂëÈçÏÂ:<script language="javascript" function f(length){alert("¸ßΪ£º"+length);}function f(length,width){alert("¸ßΪ£º"+length+"£¬¿íΪ£º"+width);}</srcipt ÉÏÃæÄǶδúÂëÆäʵÊÇÐв»Í¨µÄ£¬ÒòΪº¯Êý¶¨ÒåʱµÄ²ÎÊý¸öÊýºÍº¯Êýµ÷ÓÃʱµÄ²ÎÊý¸öÊýûÓÐÈκιØϵ¡£ ÔÚº¯ÊýÖпÉÒÔÓÃf.arguments[0]ºÍf.arguments[1]µÃµ½µ÷ÓÃʱ´«ÈëµÄµÚÒ»ºÍµÚ¶þ¸ö²ÎÊý£¬ËùÒÔ¶¨Òåfunction(length)£¬ºóÃæÓÃf(10,10)µ÷ÓÃÊÇûÓÐÎÊÌâµÄ¡£ËùÒÔÔÚÉÏÃæÕâ¶Î´úÂëÖУ¬µÚ¶þ¸öº¯ÊýÊÇÓÀÔ¶²»¿ÉÄܱ»µ÷Óõ½µÄ£¬ÄÇô£¬ÒªÔõÑù²ÅÄÜʵÏÖÏñº¯ÊýÖØÔØÄÇÑùµÄ¹¦ÄÜÄØ£¿ ÄǾÍÊÇÔÚº¯Êý¶¨ÒåÖÐÓÃf.arguments.lengthÅжÏһϵ÷ÓÃʱ´«ÈëµÄ²ÎÊý¸öÊý¡£È»ºó¶Ô²»Í¬µÄÇé¿ö²ÉÓò»Í¬µÄ´¦Àí·½Ê½¡£ ÈçÏ£º¸´ÖÆ´úÂë´úÂëÈçÏÂ:<script language="javascript" function f(){var len= arguments.length; if(1 == len){var length = arguments[0]; var width = arguments[1]; f2(length,width);}else{var length = arguments[0]; f1(length);}}function f1(length){alert("¸ßΪ£º"+length);}function f2(length,width){alert("¸ßΪ£º"+length+"£¬¿íΪ£º"+width);}</srcipt ÕâÑù£¬Äã¾Í¿ÉÒÔ¸øº¯Êýf()´«ÈëÒ»¸ö²ÎÊýÒ²¿ÉÒÔ´«ÈëÁ½¸ö²ÎÊýÁË£¬±ÈÈçf(10)ºÍf(10,10)£» ¸öÈ˾õµÃ£¬ÕâÑùËäÈ»¿ÉÒÔʵÏÖÖØÔØ£¬µ«Ò²²»ÊǺܺÃÓã¬ÎÒÃÇ¿ÉÒÔ¸ù¾Ý¾ßÌåÇé¿öÔÚÒ»¸öº¯ÊýÖÐʵÏÖÖØÔØ£¬Èç¹ûÒªÖØÔصÄÁ½¸öº¯ÊýÏà²î½Ï´ó£¬ÄǾͱ£ÁôÁ½¸öº¯Êý£¬¶øÈç¹ûÁ½¸öº¯ÊýµÄʵÏÖ»ù±¾²î²»¶à£¬ÄÇô¿ÉÒÔÔÚÒ»¸öº¯ÊýÖнøÐÐÅжϣ¬´¦Àí²»Í¬µÄ²¿·Ö£¬¶ø²»ÐèÒªÏñÉÏÃæÄÇÑùд³ÉÈý¸öº¯Êý£¬ÈçÏ£º¸´ÖÆ´úÂë´úÂëÈçÏÂ:<script language="javascript" function f(length){var len= arguments.length; alert("¸ßΪ£º"+length+"£¬¿íΪ£º"+width);}else{alert("¸ßΪ£º"+length);}}</srcipt
ɨһɨ¶þάÂëÓÃÊÖ»úÔĶÁ