{"id":141,"date":"2009-10-25T09:36:31","date_gmt":"2009-10-25T01:36:31","guid":{"rendered":"http:\/\/tmp.xujiwei.com\/blog\/?p=141"},"modified":"2010-01-29T13:11:37","modified_gmt":"2010-01-29T05:11:37","slug":"autocallback-by-arguments-caller","status":"publish","type":"post","link":"https:\/\/xujiwei.com\/blog\/2009\/10\/autocallback-by-arguments-caller\/","title":{"rendered":"\u4f7f\u7528 arguments.caller \u5b9e\u73b0\u81ea\u52a8\u56de\u8c03"},"content":{"rendered":"<div class=\"post-content\">\n<p>\u8fd9\u662f\u4e00\u7bc7\u4ecb\u7ecd\u7c7b\u4f3c\u4e8e\u00a0js\u00a0hack\u00a0\u7684\u6587\u7ae0\uff0c\u53ea\u662f\u8bf4\u660e\u4e86\u4e00\u79cd\u53ef\u884c\u7684\u9014\u5f84\uff0c\u5e76\u4e14\u53ef\u80fd\u4f1a\u589e\u52a0\u4ee3\u7801\u590d\u6742\u7a0b\u5ea6\uff0c\u5177\u4f53\u9879\u76ee\u4e2d\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u8fd8\u8bf7\u81ea\u8fa8\uff1a\uff09<\/p>\n<p>\u5728\u524d\u7aef\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u6709\u8bb8\u591a\u4e1a\u52a1\u6d41\u7a0b\u53ef\u80fd\u662f\u9700\u8981\u7528\u6237\u8fdb\u884c\u767b\u5f55\u7684\uff0c\u5e76\u4e14\u767b\u5f55\u8fc7\u7a0b\u662f\u653e\u5728\u5f39\u51fa\u5c42\u4e2d\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u4e0d\u7528\u5237\u65b0\u9875\u9762\uff0c\u589e\u5f3a\u7528\u6237\u4f53\u9a8c\u3002\u5728\u767b\u5f55\u65f6\uff0c\u7528\u6237\u7684\u64cd\u4f5c\u5c31\u4f1a\u88ab\u6253\u65ad\uff0c\u4e3a\u4e86\u8fdb\u4e00\u6b65\u589e\u5f3a\u7528\u6237\u4f53\u9a8c\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5728\u767b\u5f55\u5b8c\u6210\u540e\u81ea\u52a8\u7ee7\u7eed\u8fdb\u884c\u7528\u6237\u5728\u767b\u5f55\u524d\u60f3\u8fdb\u884c\u7684\u64cd\u4f5c\u3002<\/p>\n<p>\u5047\u8bbe\u6709\u8fd9\u6837\u4e00\u4e2a\u573a\u666f\uff0c\u7528\u6237\u9700\u8981\u53d1\u8868\u4e00\u4e2a\u7559\u8a00\uff0c\u4f46\u662f\u53d1\u8868\u7559\u8a00\u662f\u9700\u8981\u767b\u5f55\u7684\uff0c\u800c\u53d1\u8868\u7559\u8a00\u7684\u8f93\u5165\u6846\u662f\u4e00\u76f4\u663e\u793a\u7684\uff0c\u8fd9\u4e5f\u5c31\u8981\u6c42\u5728\u7528\u6237\u70b9\u51fb\u4e86\u53d1\u8868\u6309\u94ae\u65f6\u5bf9\u7528\u6237\u767b\u5f55\u72b6\u6001\u8fdb\u884c\u9a8c\u8bc1\uff0c\u4f20\u7edf\u7684\u505a\u6cd5\u662f\u5c06\u5728\u7528\u6237\u767b\u5f55\u72b6\u6001\u68c0\u67e5\u5c01\u88c5\u5728\u4e00\u4e2a\u51fd\u6570\u4e4b\u4e2d\uff0c\u8fd9\u4e2a\u51fd\u6570\u63a5\u6536\u4e00\u4e2a\u56de\u8c03\u53c2\u6570\uff0c\u5982\u679c\u767b\u5f55\u9a8c\u8bc1\u901a\u8fc7\uff0c\u5219\u6267\u884c\u56de\u8c03\u51fd\u6570\u3002\u8fd9\u6837\u7684\u903b\u8f91\u53ef\u4ee5\u7528\u4ee5\u4e0b\u4ee3\u7801\u8868\u793a\uff1a<\/p>\n<div class=\"codeHead\">\u7a0b\u5e8f\u4ee3\u7801\uff1a<a href=\"javascript:CopyText($('CODE_4316'));\">[ \u590d\u5236\u4ee3\u7801\u5230\u526a\u8d34\u677f ]<\/a><\/div>\n<blockquote>\n<div id=\"CODE_4316\" class=\"codeMain\"><span style=\"font-style: normal;\">function\u00a0doAction()\u00a0{<br \/>\ncheckLogin(function()\u00a0{<br \/>\n\/\/\u00a0\u5904\u7406\u4e1a\u52a1\u903b\u8f91<br \/>\n});<br \/>\n}<br \/>\nfunction\u00a0checkLogin(callback)\u00a0{<br \/>\nif\u00a0(isLogin)\u00a0{<br \/>\ncallback();<br \/>\n}\u00a0else\u00a0{<br \/>\nshowLogin(callback);<br \/>\n}<br \/>\n}<br \/>\nfunction\u00a0showLogin(callback)\u00a0{<br \/>\ndocument.getElementById(\"login-btn\").onclick\u00a0=\u00a0function()\u00a0{<br \/>\nisLogin\u00a0=\u00a0true;<br \/>\ncallback();<br \/>\n};<br \/>\n}<\/span><\/p>\n<ul><\/ul>\n<\/div>\n<\/blockquote>\n<p>\u603b\u89c9\u5f97\u8fd9\u6837\u7684\u65b9\u5f0f\u4f1a\u5c06\u4e1a\u52a1\u903b\u8f91\u653e\u5230\u4e00\u4e2a\u533f\u540d\u51fd\u6570\u4e2d\uff0c\u800c\u4e0d\u662f\u653e\u5728\u4e86\u6309\u94ae\u7684\u4e8b\u4ef6\u54cd\u5e94\u51fd\u6570\u4e2d\uff0c\u611f\u89c9\u4e0a\u4e0d\u662f\u90a3\u4e48\u597d\u3002\u8fd9\u5bf9\u6574\u4e2a\u4e8b\u4ef6\u54cd\u5e94\u51fd\u6570\u7684\u6539\u52a8\u6bd4\u8f83\u5927\uff0c\u4e3b\u8981\u7684\u4e1a\u52a1\u903b\u8f91\u90fd\u653e\u5728\u4e86\u767b\u5f55\u9a8c\u8bc1\u51fd\u6570\u7684\u56de\u8c03\u4e2d\u3002\u800c\u5e0c\u671b\u53ef\u4ee5\u662f\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n<div class=\"codeHead\">\u7a0b\u5e8f\u4ee3\u7801\uff1a<a href=\"javascript:CopyText($('CODE_4415'));\">[ \u590d\u5236\u4ee3\u7801\u5230\u526a\u8d34\u677f ]<\/a><\/div>\n<blockquote>\n<div id=\"CODE_4415\" class=\"codeMain\"><span style=\"font-style: normal;\">function\u00a0doAction()\u00a0{<br \/>\n\/\/\u00a0\u76f4\u63a5\u5728\u51fd\u6570\u5f00\u59cb\u8fdb\u884c\u767b\u5f55\u9a8c\u8bc1\uff0c\u5c06\u4e1a\u52a1\u903b\u8f91\u72ec\u7acb\u51fa\u6765<br \/>\nif\u00a0(!doLogin())\u00a0{<br \/>\nreturn;<br \/>\n}<br \/>\n\/\/\u00a0\u5904\u7406\u4e1a\u52a1\u903b\u8f91<br \/>\n}<br \/>\nfunction\u00a0doLogin()\u00a0{<br \/>\nif\u00a0(isLogin)\u00a0{<br \/>\nreturn\u00a0true;<br \/>\n}\u00a0else\u00a0{<br \/>\nvar\u00a0callback\u00a0=\u00a0function()\u00a0{};\u00a0\/\/\u00a0\u81ea\u52a8\u7ec4\u88c5\u00a0callback<br \/>\nshowLogin(callback);<br \/>\nreturn\u00a0false;<br \/>\n}<br \/>\n}<br \/>\nfunction\u00a0showLogin(callback)\u00a0{<br \/>\ndocument.getElementById(\"login-btn\").onclick\u00a0=\u00a0function()\u00a0{<br \/>\nisLogin\u00a0=\u00a0true;<br \/>\ncallback();<br \/>\n};<br \/>\n}<\/span><\/p>\n<ul><\/ul>\n<\/div>\n<\/blockquote>\n<p>\u8fd9\u91cc\u7684\u5173\u952e\u5728\u4e8e\u600e\u4e48\u201c\u81ea\u52a8\u7ec4\u88c5\u00a0callback\u201d\uff0c\u5f88\u5e78\u8fd0\u7684\u662f\uff0cJavaScript\u00a0\u7684\u00a0Function\u00a0\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5c5e\u6027\u00a0caller\u00a0\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u8c03\u7528\u5f53\u524d\u51fd\u6570\u7684\u51fd\u6570\u662f\u4ec0\u4e48\u3002\u5e76\u4e14\u8fd8\u53ef\u4ee5\u7528\u00a0caller\u00a0\u7684\u00a0arguments\u00a0\u5c5e\u6027\u6765\u83b7\u53d6\u00a0caller\u00a0\u6267\u884c\u65f6\u7684\u53c2\u6570\u662f\u4ec0\u4e48\uff0c\u8fd9\u4e5f\u5c31\u4f7f\u6211\u4eec\u81ea\u52a8\u7ec4\u88c5\u00a0callback\u00a0\u5e76\u6062\u590d\u00a0caller\u00a0\u7684\u539f\u53c2\u6570\u6210\u4e3a\u53ef\u80fd\u3002<\/p>\n<blockquote>\n<p style=\"text-align: auto;\">\n<p><span style=\"font-family: 'Lucida Grande', 'Lucida Sans Unicode', Arial, Helvetica, Sans, FreeSans, Jamrul, Garuda, Kalimati; color: #000000;\"><\/p>\n<div id=\"_mcePaste\">\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\">&lt;div id=\"result\"&gt;&lt;\/div&gt;<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\">&lt;input type=\"button\" value=\"Sumit\" onclick=\"sendPost()\" id=\"post\" \/&gt;<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\">&lt;input type=\"button\" value=\"Clear login\" onclick=\"isLogin=false\" id=\"reset\" \/&gt;<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\">&lt;div id='login' style=\"border:1px solid #000;padding:10px;margin:10px;display:none\"&gt;<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> &lt;strong&gt;Login&lt;\/strong&gt;&lt;br \/&gt;<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> &lt;input type=\"button\" id='loginbtn' value=\"Login\" \/&gt;<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\">&lt;\/div&gt;<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\">&lt;script type=\"text\/javascript\"&gt;<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\">\/\/ \u662f\u5426\u767b\u5f55<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\">var isLogin = false;<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\">\/\/ \u68c0\u67e5\u767b\u5f55\u72b6\u6001<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\">var checkLogin = function(cfg) {<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> if (isLogin) {<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> return true;<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> }<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> cfg = cfg || {};<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> var callback = null;<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> \/\/ \u81ea\u52a8\u6267\u884c\u56de\u8c03<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> if (cfg.autoCallback &amp;&amp; arguments.callee.caller) {<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> try {<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> var caller \u00a0= arguments.callee.caller,<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> args \u00a0 \u00a0= caller.arguments || [],<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> scope \u00a0 = cfg.callbackScope || {},<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> newArgs = [];<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> for (var i = 0; i &lt; args.length; ++i) {<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> newArgs.push(args[i]);<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> }<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> callback = caller ? function() {<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> try {<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> caller &amp;&amp; caller.apply(scope, newArgs);<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> } catch (e) {<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> \/\/alert(e.message);<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> }<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> } : null;<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> } catch (e) {<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> callback = null;<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> }<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> }<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> <\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> showLogin(callback);<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> <\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> return false;<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\">};<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\">function sendPost() {<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> if (!checkLogin({autoCallback:true})) {<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> return;<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> }<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> <\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> document.getElementById('result').innerHTML += 'Hello&lt;br \/&gt;';<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\">}<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\">function showLogin(callback) {<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> document.getElementById('login').style.display = ';<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> document.getElementById('loginbtn').onclick = function() {<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> document.getElementById('login').style.display = 'none';<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> isLogin = true;<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> callback &amp;&amp; callback();<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\"> }<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\">}<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"font-style: normal;\">&lt;\/script&gt;<\/span><\/div>\n<\/div>\n<p><\/span><\/p><\/blockquote>\n<p>\u5176\u5b9e\u5f04\u51fa\u8fd9\u4e48\u4e2a\u65b9\u6cd5\u6765\u5b9e\u73b0\u767b\u5f55\u81ea\u52a8\u56de\u8c03\u4e5f\u662f\u4e3a\u4e86\u65b9\u4fbf\u5199\u4ee3\u7801\uff0c\u4e0d\u7528\u6bcf\u6b21\u53bb\u5c06\u4e00\u5806\u4e1a\u52a1\u903b\u8f91\u653e\u5230\u00a0callback\u00a0\u4e2d\uff0c\u76f4\u63a5\u5728\u51fd\u6570\u5f00\u59cb\u56de\u4e00\u4e2a\u00a0if\u00a0(!checkLogin({autoCallback:true}))\u00a0\u5c31\u884c\u3002<\/p>\n<p>caller\u00a0\u5c5e\u6027\u5728\u00a0MDC\u00a0\u4e2d\u88ab\u6807\u8bb0\u4e3a\u201c\u975e\u6807\u51c6\u201d\u7684\uff0c\u4f46\u662f\u5728\u4e3b\u6d41\u6d4f\u89c8\u5668\u4e2d\uff0c\u90fd\u662f\u652f\u6301\u8fd9\u4e2a\u5c5e\u6027\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728\u6d4f\u89c8\u5668\u517c\u5bb9\u6027\u4e0a\u4f7f\u7528\u00a0caller\u00a0\u662f\u6ca1\u6709\u95ee\u9898\u7684\u3002<\/p>\n<p>\u4e0d\u8fc7\uff0c\u4f7f\u7528\u5947\u6280\u6deb\u5de7\u5bb9\u6613\u4f24\u8eab\u4f53\uff0c\u614e\u7528\uff1a\uff09<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u662f\u4e00\u7bc7\u4ecb\u7ecd\u7c7b\u4f3c\u4e8e\u00a0js\u00a0hack\u00a0\u7684\u6587\u7ae0\uff0c\u53ea\u662f\u8bf4\u660e\u4e86\u4e00\u79cd\u53ef\u884c\u7684\u9014\u5f84\uff0c\u5e76\u4e14\u53ef\u80fd\u4f1a &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/xujiwei.com\/blog\/2009\/10\/autocallback-by-arguments-caller\/\">\u7ee7\u7eed\u9605\u8bfb &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[11],"tags":[99,98,28],"_links":{"self":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/posts\/141"}],"collection":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/comments?post=141"}],"version-history":[{"count":0,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/posts\/141\/revisions"}],"wp:attachment":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/media?parent=141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/categories?post=141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/tags?post=141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}