[Ãâ·Ñ×¢²á][°²È«µÇ¼]
µ±Ç°Î»ÖãºÊ×Ò³¡úÓîÖæ̽Ë÷¡újsÈçºÎʵÏÖº¯ÊýÖØÔØ,jsÖØÔØ

ÕýÔÚ¼ÓÔØ×îÐÂÊý¾Ý£¬ÇëÉÔµÈ......

jsÈçºÎʵÏÖº¯ÊýÖØÔØ,jsÖØÔØ

ÓîÖæ̽Ë÷2023-6-23ÔĶÁ£º398

640

º¯ÊýÖØÔØ

ÔÚÆäËûÓïÑÔÖУ¬ÎÒÃÇÒ»°ã¶¼Ìý˵¹ýÖØÔصĸÅÄ¶ÔÓÚÒ»¸ö·½·¨£¬Èç¹ûÎÒÃÇÆÚÍû´«È벻ͬµÄ²ÎÊýÀàÐÍ£¬»òÕß´«È벻ͬµÄ²ÎÊý¸öÊý£¬ÉõÖÁ´«Èë²ÎÊýµÄ˳Ðò²»Ò»Ñù£¬¶øÈ¥Ö´Ðв»Í¬µÄ´¦Àí´úÂ룬ÒÔ·µ»ØÏàÓ¦µÄ½á¹û£¬ÄÇôËûÃǵķ½·¨ÃûÊÇ¿ÉÒÔÏàͬµÄ£¬¶øÇÒËüÃDz»»á²úÉú³åÍ»£¬Õâ¾ÍÊÇËùνµÄÖØÔØ¡£

ÕâÊÇÒòΪÏñjavaµÈÕâÑùµÄÇ¿ÀàÐÍÓïÑÔ£¬ËûÃǾßÓÐÀàÐÍÔ¼ÊøºÍ·½·¨Ç©Ãû£¬ÕâÑù¾ÍʹµÃËûÃÇÄܹ»¸ù¾Ýµ÷ÓÃʱ´«Èë²ÎÊýµÄÇé¿öÀ´¾ö¶¨Ê¹ÓÃÄÄÒ»¸öͬÃû·½·¨À´´¦ÀíÐèÒªµÄ²Ù×÷¡£

640

ÊÇÔÚ˵ÎÒÂð£¿

µ«ÊÇjsÊôÓÚÈõÀàÐÍÓïÑÔ£¬Ò²¾ÍÊÇ˵Ëü²»»á¹æ¶¨²ÎÊý±ØÐë´«ÈëÄÄÖÖÀàÐÍ£¬¶¨ÒåµÄ·½·¨Ò²²»¾ßÓÐÇ©ÃûµÄ¹¦ÄÜ£¬¶øÇÒÔÚ¶¨Òå¶à¸öͬÃû·½·¨Ê±£¬ÀàËÆÓÚcssÀïÃæµÄ²ãµþÑùʽ±íµÄЧ¹û£¬ºó¶¨ÒåµÄͬÃû·½·¨»á¸²¸ÇÇ°ÃæµÄ·½·¨£¬»¹ÓÐÒ»¸ö¾ÍÊǺ¯Êý¾ßÓÐÌáÉýµÄÌØÐÔ£¬ÕâÒ²¾ÍʹµÃËüÎÞ·¨ÊµÏÖÖØÔصŦÄÜ¡£

640

Ôõô°ìÄØ£¿

ÆäʵjsÖÐҲȷʵ²»ÐèÒªÖØÔصŦÄÜ£¬ÒòΪûÓÐÀàÐ͵ÄÔ¼Êø£¬ÔÚÒ»¸ö·½·¨ÀïÃæ¾Í¿ÉÒÔ×öºÜ¶à×ÔÓɵķ¢»Ó£¬²»µ«ÄÜÂú×ãÐèÒªÖØÔصÄÐèÇ󣬶øÇÒ»¹ÄÜÍæһЩ»¨Ñù¡£

²»¹ý»°ÓÖ˵»ØÀ´£¬Ã»ÓÐÁËÔ¼Êø£¬¾ÍÈÝÒ×·¸´í£¬¶¼ÓÃÒ»¸ö·½·¨ÌåÀ´´¦ÀíËùÓÐÇé¿ö£¬¾Í»áÈÝÒ׳öÂÒ×Ó£¬Ê¹µÃÎÒÃÇÐèҪʹÓÃÒ»¶ÑµÄÀàËÆif-elseµÄÓï¾äÀ´×öµ½ÕâÒ»µã¡£

ÄÇôÎÒÃÇÄܲ»ÄÜÔÚÏÖÓÐjsÔËÐз½Ê½µÄ»ù´¡ÉÏ£¬½è¼øÆäËûÓïÑÔ¶ÔÓÚÖØÔصÄÔËÓã¬À´ÈƸöÍä×Ó±äÁ¿À´ÊµÏÖÒ»ÏÂÊôÓÚjs×Ô¼ºµÄÖØÔØ·½Ê½ÄØ£¿

640

ÊÔÒ»ÊÔ

ÎÒÃǽñÌìÖ»ÌÖÂÛÔÚjsÖбäÏàʵÏÖÖØÔصķ½Ê½£¬¶ø²»ÌÖÂÛËüµÄÒâÒåÓëʵ¼ÊÓ¦Óó¡¾°£¬ÎÒÃÇͨ¹ýÒ»¸ö¼òµ¥µÄÀý×Ó£¬À´Ö§³ÅÕâ¸öÌÖÂÛ£¬ÆäËûµÄ½»ÓÉÄãÃÇÀ´×ÔÓÉ·¢»Ó¡£

640

ÈÃÎÒÃÇ¿ªÊ¼°É

jsÖØÔصķ½Ê½

ÔÚjsÖÐÖ÷ÒªÓÐÒÔϼ¸ÖÖʵÏÖÖØÔصķ½Ê½£º

ʹÓÃÊ£Óà²ÎÊýµÄÐÎʽÀ´½ÓÊտɱäµÄ²ÎÊý£¬²¢Í¨¹ýһЩÅжÏÊÖ¶ÎÀ´´¦Àí²»Í¬Çé¿öµÄÂß¼­¡£Ê¹ÓÃargumentsµÄÐÎʽ£¬À´¶¯Ì¬ÅжÏÐèÒªÖ´ÐеIJÙ×÷ʹÓÃproxyµÄÐÎʽÀ´À¹½Øº¯ÊýµÄÐÐΪ£¬ÒÔ´ïµ½¿ØÖƲ»Í¬µÄÂß¼­·ÖÖ§¡£

Ç°Á½ÖÖ·½Ê½±È½ÏÏàËÆ£¬Ë¼Â·Ò»Ñù£¬Ö»ÊÇʵÏÖÊÖ¶ÎÓÐËù²»Í¬£¬Óû§ÐèÒª×Ô¼ºÐ´ÅжÏÂß¼­¡£µÚÈýÖÖ·½Ê½½áºÏproxyÀ´Òþ²ØʵÏÖϸ½Ú£¬ÈÃÓû§Ö»¹Ø×¢¸÷×Եķֹ¤¡£µ«ËüÃǶ¼ÊÇÔÚ²ÎÊý¶¯Ì¬ÅжϷ½Ãæ×öÎÄÕ¡£

Ç°Á½ÖÖ·½Ê½µÄÓÅȱµã£º

Óŵ㣺¿ÉÒÔÖ±½Ó¶¨Ò庯Êý»òʹÓñí´ïʽ

ȱµã£ºº¯ÊýÃû²»ÄÜÏàͬ£¬ÐèҪд½Ï¶àµÄÅжϷÖÖ§

µÚÈýÖÖ·½Ê½µÄÓÅȱµã£º

Óŵ㣺¿ÉÒÔ²»ÓÃд¸÷ÖÖ²ÎÊýÐÎʽµÄ·ÖÖ§

ȱµã£ºÖ»ÄÜʹÓñí´ïʽ¶¨Ò庯Êý

ÓÉÓÚÇ°Á½ÖÖÍøÉÏÒѾ­ÓкܶàµÄʵÏÖ˼·Óë·½°¸£¬Òò´ËÕâÀï²»ÔÙ½øÐÐ̽ÌÖ£¬ÆäÖÐÓкܶàÆæÃîµÄʵÏÖ£¬¿ÉÒÔ×öµ½ÔÚjsÖÐʹÓÃÖØÔصÄ˼Ïë¡£

ËùÒÔÔÚ´ËÎÒÃÇÖ»ÌÖÂÛµÚÈýÖÖ·½°¸£¬ÎÒÃÇÏÂÃæÀ´¿´Ò»ÏÂËüµÄ˼·ÊÇʲô£¬ÊÇ·ñÂú×ãÖØÔصÄÐèÇó£¬ËüÊÇÈçºÎʵÏֵģ¬ÒÔ¼°ËüÄÜÂú×ãÎÒÃÇʲôÑùµÄÐèÇó£¿

640

ÕâÊÇʲôÄØ£¿

ÐèÇó¼ÙÉè

ÎÒÃÇÏÖÔÚÓÐÕâÑùÒ»¸ö³¡¾°ºÍÐèÇó£º

×Ô¼º¿ªÁËÒ»¼Ò·þ×°µê£¬ÓÉÓÚÉúÒâ»ð±¬£¬ÎÒÃÇÏëÒª´ðлÐÂÀϹ˿ͣ¬ÏÖÔÚÍƳöÁËÒ»¸ö»î¶¯£¬È«³¡ÎÞÂÛÈκηþ×°£¬Ö»ÒªÂòÒ»¼þ¾ÍÖ±½Ó´ò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¡¾°£¬¶ø²»ÐèÒª¶îÍâÉè¼ÆÖØÔصĴúÂë¡£

¾ßÌåÕâÖÖ·½Ê½µÄÓÅÁÓ£¬´ó¼Ò¿ÉÒÔ×ÔÐÐÅжϣ¬²¢ÇÒ¿ÉÒÔ¸ù¾ÝÕâÖÖ˼·ÖØÐÂÉè¼ÆÒ»ÏÂʵÏÖµÄÊֶΡ£

640

лл

jsÈçºÎʵÏÖº¯ÊýÖØÔØ,jsÖØÔØ

jsÖз½·¨ÖØÔØÈçºÎʵÏÖ£¿ÒÔ¼°º¯ÊýµÄ²ÎÊýÎÊÌâ

¶¼ÖªµÀÔÚjsÖÐûÓа취ֱ½ÓʵÏÖ·½·¨ÖØÔØ£¬ÒòΪÔÚjsÖÐÈç¹û¶¨ÒåÁ˶à¸öÃû³ÆÏàͬ£¬µ«²ÎÊý¸öÊý²»Ò»ÑùµÄ·½·¨£¬ÆäʵֻÓÐ×îºóÒ»¸ö·½·¨Äܱ»ÕæÕýµ÷Óã¬ÆäËûµÄ·½·¨¶¼±»¸²¸ÇµôÁË¡£
µ«Ã¿Ò»¸öº¯Êý¶¼ÓÐÒ»¸öÌØÊâµÄ²ÎÊýarguments£¬ÀûÓÃËü¿ÉÒÔʵÏÖ·½·¨µÄÖØÔØ¡£
ÀýÈ磺
¸´ÖÆ´úÂë
´úÂëÈçÏÂ:
function
Add(firstnumber,sencondnumber)
{
return
firstnumber+sencondnumber;
}
Ö»ÄÜ´¦ÀíÁ½¸ö²ÎÊý£¬Èç¹ûÓжà¸ö²ÎÊý£¬»òÕßûÓвÎÊý£¬Ò»¸ö²ÎÊýµÄÇé¿ö¶¼ÊÇ´¦Àí²»Á˵ġ£Èç¹ûûÓд«µÝ²ÎÊý£¬Ôòfirstnumber,sencondnumber¶¼ÊÇ䶨ÒåµÄ£¬Èç¹û´«µÝÁËÒ»¸ö²ÎÊý£¬¾ÍÏ൱ÓÚÖ»¸øfirstnumber¸³ÖµÁË£¬sencondnumberÒÀÈ»ÊÇ䶨Òå¡£Ïà·´Èç¹û´«µÝÁ˶àÓÚÁ½¸öµÄ²ÎÊý£¬ÔòÏ൱ÓÚfirstnumber,sencondnumber¶¼¸³ÖµÁË£¬ËäÈ»»¹ÓÐÆäËûµÄ²ÎÊý£¬µ«´¦Àíʱ¶¼ºöÂÔµôÁË¡£Èç¹ûÄÜ»ñÈ¡ÆäËûµÄ²ÎÊý£¬×ÔÈ»¾Í¿ÉÒÔ´¦ÀíÁË¡£´ËʱӦ¸Ã¾Í¿ÉÒÔÏëµ½º¯ÊýµÄÌØÊâ²ÎÊýarguments£¬Õâ¸ö°üº¬ÁË´«µÝ¸øº¯ÊýµÄËùÓвÎÊý£¬ÀûÓÃËü¾Í¿ÉÒÔʵÏÖ·½·¨ÖØÔصÄЧ¹û¡£
ÒÔÉϵķ½·¨ÐÞ¸ÄÈçÏ£º
¸´ÖÆ´úÂë
´úÂëÈçÏÂ:
function
Add(firstnumber,sencondnumber)
{
if
(arguments.length
==
0)//ûÓд«µÝ²ÎÊý
{
return
null;
}
else
if
(arguments.length
==
1)
{//´«µÝµÄÊÇÒ»¸ö²ÎÊý
return
firstnumber;//Ò²¿ÉÒÔдΪ
return
arguments[0];
}
else
if(arguments.length
==
2)//´«µÝµÄÊÇÁ½¸ö²ÎÊý
{
return
firstnumber+sencondnumber;//Ò²¿ÉÒÔдΪ
return
arguments[0]+arguments[1];
}
else
{
var
total=0;
for
(var
i
=
0;
i
<
arguments.length;
i++)
{
total=total+arguments[i]
}
return
total;
}
}
µ±È»ÕâÖÖ·½·¨µÄ±×¶Ë¾ÍÊDzÎÊýµÄ˳Ðò²»ÄÜ´òÂÒ£¬Èç¹ûº¯ÊýʵÏÖÒÀÀµÓÚ²ÎÊýµÄ˳Ðò£¬¾Í±ØÐë½øÐÐÌØÊâ´¦Àí£¬ÀýÈç´«µÝnullÀ´Õ¼Î»¡£
ÓÉÓÚ´«µÝ¸øº¯ÊýµÄ²ÎÊýÊÇÑϸñ°´ÕÕ¶¨Ò庯ÊýµÄ˳Ðò¸øÿһ¸ö²ÎÊý¸³ÖµµÄ£¬Èç¹ûÖ»Ïë¸øµÚ¶þ¸ö²ÎÊý¸³Öµ£¬Ôò±ØÐë´«µÝÁ½¸ö²ÎÊý£¬·ñÔòʵ¼ÊÉÏ´«µÝµÄÖµ¸³Öµ¸øÁ˵ÚÒ»¸ö²ÎÊý£¬²¢Ã»Óи³Öµ¸øµÚ¶þ¸ö²ÎÊý¡£
ÀýÈçÖ»Ïë¸øsencondnumber´«Öµ£¬µ«²»Ïë¸øfirstnumber´«Öµ£¬±ØÐëÕâÑùµ÷ÓÃAdd(null,2)£¨µ±È»º¯ÊýÄÚ²¿±ØÐë´¦Àí´«µÝÌØÊâÖµµÄÇé¿ö£©,Èç¹ûÕâÑùµ÷ÓÃAdd(2),ÆäʵÊǸøfirstnumber´«ÖµÁË£¬Ï൱ÓÚµ÷ÓÃÁË´«µÝÁËÒ»¸ö²ÎÊýµÄÇé¿ö¡£

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

 

ɨһɨ¶þάÂëÓÃÊÖ»úÔĶÁ

ÆÕÄþÄÚ¿ãÍø
¹ºÂò¹ã¸æÇëÁªÏµÕ¾³¤£¡
ͼƬÍƼö
    Ó¡¶È¾³ÄÚ¾¹´æÔÚÒ»¸öÁù¿ÅÐÇÖйú

    Ó¡¶È¾³ÄÚ¾¹´æÔÚÒ»¸öÁù¿ÅÐÇÖйú

    ÁùÐǺìÆ죿ɽկµÄ°É£¬¿ÉϧÕâ´ÎÄã´íÁË£¡ÕâÆ쾹ȻÊÇÕæµÄ£¡ÈçÄúËùÔ¸£¬»¹ÕæÓëÖйúÓÐÔ¨Ô´£¡¡¡¡¡°¢Èý¹ú¿¿½üÃåµé£¬¾ÍÊÇÔÚÃϼÓÀ­¶«±ßÍ»³ö²¿·ÖµÄÄǸö°ï£¬½ÐÂüÄáÆÕ¶û£¬Õâ¾ÍÊÇÕâ¸ö°îµÄ¶ÀÁ¢Ô˶¯ÆìÖÄ¡£¡¡¡¡ÒªËµÆðÂüÄáÆÕ¶ûºÍÖйúµÄ
    Ë­ÖªµÀ°ìÖ¤µÄ?,°ìÖ¤µÄ¹ÊÊÂ

    Ë­ÖªµÀ°ìÖ¤µÄ?,°ìÖ¤µÄ¹ÊÊÂ

    ÍøÉÏÓкܶà¹ØÓÚË­ÖªµÀ°ìÖ¤µÄ?,°ìÖ¤µÄ¹ÊʵÄ֪ʶ£¬Ò²ÓкܶàÈËΪ´ó¼Ò½â´ð¹ØÓÚ°ìÖ¤ÆæÎŵÄÎÊÌ⣬Ϊ´ó¼ÒÕûÀíÁ˹ØÓÚÕâ·½ÃæµÄ֪ʶ£¬ÈÃÎÒÃÇÒ»ÆðÀ´¿´Ï°É!±¾ÎÄĿ¼һÀÀ£º1¡¢°ìÖ¤ÆæÎÅ°ìÖ¤ÆæÎŽüÈÕ£¬¹ØÓÚºþ±±Ê¡¼Íίԭ¸±ch
    Öйú¹Å´úÐÔ±ä̬£ºÁµÊÞñ±ÁµÊ¬ñ±ËÁÐÐ

    Öйú¹Å´úÐÔ±ä̬£ºÁµÊÞñ±ÁµÊ¬ñ±ËÁÐÐ

    ¡¡¡¡±ð˵Á½ÐÔÈËÕâÖÖÎÒÃǽñÌìÄܹ»ÓÐËù¶úÎŵÄÏÖÏ󣬹ÅÈËÔÚÈËÊÞÐÔ½»¡¢ÈËʬ½»»¶ÉÏÃæµÄ³ö¸ñÖ®¾Ù¸üÊÇÈÃÎÒºÍÎÒµÄС»ï°éÃǾªµÃ»ê²»¸½Ì壡ÈËÀàµÄÐÔ°üÀ¨ÉúÀí¡¢ÐÄÀí¡¢Éç»áÈýά¶È¡£ÐԵĽ¡¿µÕý³£Óë·ñ£¬¶¼ÊÜÕâ
    ´æÁ¿·¿´ûÀûÂÊϵ÷ϸÔò·¢²¼ Ô¹©Ö§³ö½µµÍÔÚÍû

    ´æÁ¿·¿´ûÀûÂÊϵ÷ϸÔò·¢²¼ Ô¹©Ö§³ö½µµÍÔÚÍû

    ¡¾×ÊÁÏͼ¡¿9ÔÂ7ÈÕ£¬¹¤ÉÌÒøÐС¢Å©ÒµÒøÐС¢ÖйúÒøÐС¢½¨ÉèÒøÐС¢½»Í¨ÒøÐкÍÓÊ´¢ÒøÐÐÒÔ¼°ÖÐÐÅÒøÐС¢ÐËÒµÒøÐС¢ÕÐÉÌÒøÐеȷ׷׹«²¼´æÁ¿·¿´ûÀûÂÊϵ÷¾ßÌå¹æÔò¡£¸ù¾ÝÒѹ«²¼ÒøÐй«¸æ£¬´Ë´Îϵ÷ÀûÂÊ£¬²»Í¬Ê±¼ä¶Î·¢·ÅµÄ´û¿î
    ¶þÕ½Òż££ºÌس¿ËкºþµÄ¡°ÓÄÁé½¢¶Ó¡±

    ¶þÕ½Òż££ºÌس¿ËкºþµÄ¡°ÓÄÁé½¢¶Ó¡±

    ¡¡¡¡Ìس¿Ëкºþ£¬¾ÍÊÇÖøÃûµÄ³þ¿Ë³Á´¬Çø£¬Î»ÓÚÃÜ¿ËÂÞÄáÎ÷ÑÇÁª°î¹ú¼ÒÖªÃûµÄÈÈ´øÌìÌõºÓìȺ£¬Ç±Ë®°®ºÃÕßÔÚÕâ¸ööèÓã³öûµÄ̫ƽÑóË®Óò¿ÉÒÔÌå»áÎÞ±ÈÇåÁ¹µÄº£Ë®£¬¶ø³þ¿ËÓÄÁé½¢¶Ó³Á´¬Ç±Ë®£¬¸üÊÇÒ»´ÎÁîÈË
    ΪʲôÀ´´óÒÌÂèϸ¹Í´,ΪʲôһÀ´´óÒÌÂè¾ÍÀ­¶Ç×Ó

    ΪʲôÀ´´óÒÌÂèϸ¹Í´,ΪʲôһÀ´´óÒÌÂè¾ÍÀ­¶Ç×Ó

    ÍøÉÏÓкܶà¹ØÓÚΪʲôÀ´´óÒÌÂèϸ¹Í´,ΪʲôһÀ´´óÒÌÂè¾ÍÀ­¶Ç×ÓµÄ֪ʶ£¬Ò²ÓкܶàÈËΪ´ó¼Ò½â´ð¹ØÓÚΪʲôÀ´´óÒÌÂèϸ¹Í´µÄÎÊÌ⣬¿´°Ù¿ÆΪ´ó¼ÒÕûÀíÁ˹ØÓÚÕâ·½ÃæµÄ֪ʶ£¬ÈÃÎÒÃÇÒ»ÆðÀ´¿´Ï°É!±¾ÎÄĿ¼һÀÀ£º1¡¢ÎªÊ²Ã´
ÄãÊÇÔõô֪µÀÖÚÈËȤµÄ£¿
  •   
  • ·µ»Ø¶¥²¿