jsÖÐevalº¯ÊýÊǸÉÂïµÄ
eval º¯ÊýÔÚ javascript ÖеÄ×÷ÓÃÊÇÖ´ÐÐ×÷Ϊ´úÂëµÄ×Ö·û´®¡£Ëü¿ÉÒÔÓÃÓÚ¶¯Ì¬ÌìÉú´úÂë¡¢Ö´Ðд洢ÔÚ×Ö·û´®ÖеĴúÂëÒÔ¼°À©Õ¹ javascript ÓïÑÔ¡£È»¶ø£¬ÓÉÓÚÇå¾²Òþ»¼¡¢ÐÔÄÜÎÊÌâºÍά»¤ÄÑÌ⣬Ӧ¸ÃÉ÷Óà eval º¯Êý£¬²¢ÇÒ¿ÉÒÔÔÚ´ó´ó¶¼ÇéÐÎÏÂÕÒµ½Ìæ»»¼Æ»®¡£
eval º¯ÊýÔÚ JavaScript ÖеÄ×÷ÓÃ
eval º¯ÊýÔÚ JavaScript Öб»ÓÃÀ´Ö´ÐÐÒ»¸ö×Ö·û´®×÷Ϊ´úÂë¡£ËüµÄÓ﷨Ϊ£º
eval(code)
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬code ÊÇÒ»¸ö×Ö·û´®£¬°üÀ¨ÒªÖ´ÐÐµÄ JavaScript ´úÂë¡£
Ó÷¨
eval º¯Êý½« code ×Ö·û´®×÷Ϊһ¸ö JavaScript ³ÌÐòÀ´Ö´ÐУ¬²¢·µ»ØÆäЧ¹û¡£ÀýÈ磺
let result = eval("1 + 2"); // ·µ»Ø 3 let name = "John"; eval(`console.log("Hello, ${name}!");`); // Êä³ö "Hello, John!"
µÇ¼ºó¸´ÖÆ
Ó¦Óó¡¾°
eval º¯ÊýÔÚijЩÇéÐÎϺÜÊÇÓÐÓã¬ÀýÈ磺
¶¯Ì¬ÌìÉú´úÂ룺µ±ÐèÒª»ùÓÚÓû§ÊäÈë»òÆäËû¶¯Ì¬ÒòËØÌìÉú´úÂëʱ£¬eval º¯Êý¿ÉÒÔÀû±ãµØʵÏÖ¡£
Ö´ÐÐ×Ö·û´®ÖеĴúÂ룺½ñÊÀÂë´æ´¢ÔÚ×Ö·û´®ÖÐʱ£¬eval º¯Êý¿ÉÒÔ½«×Ö·û´®×ª»»Îª¿ÉÖ´ÐдúÂë¡£
À©Õ¹ JavaScript ÓïÑÔ£ºeval º¯ÊýÔÊÐí¶¯Ì¬µØ½«Ð¹¦Ð§Ìí¼Óµ½ JavaScript ÖС£
×¢ÖØ
ËäÈ» eval º¯ÊýºÜÇ¿Ê¢£¬µ«Ò²ÐèҪעÖØÒÔϼ¸µã£º
Çå¾²Òþ»¼£ºeval º¯ÊýÖ´ÐÐ×Ö·û´®ÖеĴúÂ룬Õâ¿ÉÄÜ»áÒýÈëÇå¾²Òþ»¼¡£ÔÚ´úÂëÀ´×Ô²»¿ÉÐÅȪԴʱ£¬²»Ó¦¸ÃʹÓà eval¡£
ÐÔÄÜÎÊÌ⣺eval º¯ÊýµÄÖ´ÐÐËÙÂʱÈÖ±½ÓÖ´ÐдúÂëÂý¡£
ά»¤ÄÑÌ⣺ʹÓà eval º¯ÊýÌìÉúµÄ´úÂë¿ÉÄÜÄÑÒÔά»¤¡£
Ìæ»»¼Æ»®
ÔÚ´ó´ó¶¼ÇéÐÎÏ£¬¿ÉÒÔʹÓÃÆäËûÌæ»»¼Æ»®À´ÊµÏÖ eval º¯ÊýµÄ¹¦Ð§£¬ÀýÈ磺
Function ½á¹¹º¯Êý£ºÔÊÐíʹÓÃ×Ö·û´®¶¯Ì¬½¨É躯Êý¡£
new Function£ºÀàËÆÓÚ Function ½á¹¹º¯Êý£¬µ«¿ÉÒÔʹÓòî±ðµÄÓï·¨¡£
indirect eval£ºÊ¹Óüä½ÓÒýÓÃÀ´ÏÞÖÆ eval º¯ÊýµÄÖ´ÐйæÄ£¡£
½áÂÛ
eval º¯ÊýÊÇÒ»¸öÇ¿Ê¢µÄ¹¤¾ß£¬¿ÉÒÔ¶¯Ì¬µØÖ´ÐÐ×Ö·û´®ÖеĴúÂë¡£¿ÉÊÇ£¬ËüÓ¦¸ÃÉóÉ÷ʹÓ㬲¢Ë¼Á¿ÆäDZÔÚµÄÇå¾²Òþ»¼ºÍÐÔÄÜÎÊÌâ¡£Ò»Ñùƽ³£ÇéÐÎÏ£¬¿ÉÒÔÕÒµ½Ìæ»»¼Æ»®À´ÊµÏÖ eval º¯ÊýµÄ¹¦Ð§¡£
ÒÔÉϾÍÊÇjsÖÐevalº¯ÊýÊǸÉÂïµÄµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡