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

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

ÓîÖæ̽Ë÷     2023-6-23     µãÆÀ:15¾Ù±¨

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
ÓîÖæ̽Ë÷ÀàÍƼöÔĶÁ ⇓
¹ØÓÚÍâÐÇÈ˵ÄÊ®´óÆæÌزÂÏ룬25ÄêÄÚÈËÀàÄÜÕÒµ½ÍâÐÇÈË

¹ØÓÚÍâÐÇÈ˵ÄÊ®´óÆæÌزÂÏ룬25ÄêÄÚÈËÀàÄÜÕÒµ½ÍâÐÇÈË

ÓîÖæ̽Ë÷ÔĶÁ:11580   µãÆÀ:363
UFOµ½µ×³¤Ê²Ã´Ñù×Ó£¿

UFOµ½µ×³¤Ê²Ã´Ñù×Ó£¿

ÓîÖæ̽Ë÷ÔĶÁ:9932   µãÆÀ:368
ľÐǵÚËÄ´óÎÀÐÇ µØµ×º£ÑóË®Á¿´óµ½¾ªÈË£¡

ľÐǵÚËÄ´óÎÀÐÇ µØµ×º£ÑóË®Á¿´óµ½¾ªÈË£¡

ÓîÖæ̽Ë÷ÔĶÁ:13588   µãÆÀ:93
¸ÎÑײ»Ò»¶¨¶¼»á´«È¾ÈË,´øÄãÈ«ÃæÁ˽â¸ÎÑ×¼²²¡

¸ÎÑײ»Ò»¶¨¶¼»á´«È¾ÈË,´øÄãÈ«ÃæÁ˽â¸ÎÑ×¼²²¡

É̼ҹã¸æÔĶÁ:7920   µãÆÀ:244
Ì«Ñô·ç±©¶ÔµØÇòÓ°Ï죺½ÒÉñÃØÁ¦Á¿À´Ô´

Ì«Ñô·ç±©¶ÔµØÇòÓ°Ï죺½ÒÉñÃØÁ¦Á¿À´Ô´

ÓîÖæ̽Ë÷ÔĶÁ:7498   µãÆÀ:232
¿Õ¼ä̬ÊÆÆÀ¹À±¨¸æ2016¡ª¡ªÎÒ¹ú»ð¼ý·¢Éä³É¹¦ÂÊλ¾ÓÊÀ½çÊ×λ

¿Õ¼ä̬ÊÆÆÀ¹À±¨¸æ2016¡ª¡ªÎÒ¹ú»ð¼ý·¢Éä³É¹¦ÂÊλ¾ÓÊÀ½çÊ×λ

ÓîÖæ̽Ë÷ÔĶÁ:7876   µãÆÀ:338
±ÈľÐÇ»¹´ó°Ë±¶µÄϵÍâÐÐÐÇ ²»¸ÒÏëÏó

±ÈľÐÇ»¹´ó°Ë±¶µÄϵÍâÐÐÐÇ ²»¸ÒÏëÏó

ÓîÖæ̽Ë÷ÔĶÁ:9773   µãÆÀ:172
Å̵ãÌ«¿ÕÆß´óÎÚÁúʼþ£º·¢ÏÖÍâÐÇÉúÃü

Å̵ãÌ«¿ÕÆß´óÎÚÁúʼþ£º·¢ÏÖÍâÐÇÉúÃü

ÓîÖæ̽Ë÷ÔĶÁ:9577   µãÆÀ:378
ÆæÃÓîÖæºì¹â¾¹¿É±¬·¢1000¸öÌ«ÑôÄÜÁ¿

ÆæÃÓîÖæºì¹â¾¹¿É±¬·¢1000¸öÌ«ÑôÄÜÁ¿

ÓîÖæ̽Ë÷ÔĶÁ:12009   µãÆÀ:272
º±¼ûÍÁÓï½öÁ½ÈË»á˵ È´Òò¹Øϵ²»ºÃ¾Ü¾ø½»Ì¸

º±¼ûÍÁÓï½öÁ½ÈË»á˵ È´Òò¹Øϵ²»ºÃ¾Ü¾ø½»Ì¸

ÓîÖæ̽Ë÷ÔĶÁ:8917   µãÆÀ:148
Õ¾³¤Í³¼Æ
·µ»Ø¶¥²¿