{"id":651,"date":"2009-01-05T17:01:01","date_gmt":"2009-01-05T09:01:01","guid":{"rendered":"http:\/\/fdream.net\/blog\/article\/651.aspx"},"modified":"2009-01-05T17:01:15","modified_gmt":"2009-01-05T09:01:15","slug":"Mootools%e5%b8%b8%e7%94%a8%e6%96%b9%e6%b3%95%e6%89%a9%e5%b1%95%ef%bc%88%e4%ba%8c%ef%bc%89","status":"publish","type":"post","link":"https:\/\/fdream.net\/blog\/article\/651","title":{"rendered":"Mootools\u5e38\u7528\u65b9\u6cd5\u6269\u5c55\uff08\u4e8c\uff09"},"content":{"rendered":"<p>\u7ee7\u7eedMootools\u5e38\u7528\u65b9\u6cd5\u6269\u5c55\uff0c\u4f9d\u7136\u8fd8\u662fString\u7c7b\u7684\u6269\u5c55\u3002<\/p>\n<h3>\u65b9\u6cd5\uff1aformat<\/h3>\n<p>\u8bf4\u660e\uff1a\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684format\u65b9\u6cd5\uff0c\u548cC#\u91cc\u9762\u7684format\u7c7b\u4f3c\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u7528\u53c2\u6570\u6765\u4f9d\u6b21\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7528\u5927\u62ec\u53f7{}\u62ec\u8d77\u6765\u7684\u6570\u5b57\uff0c\u6bd4\u5982\u7528\u7b2c\u4e00\u4e2a\u53c2\u6570\u66ff\u6362{0}\uff0c\u7528\u7b2c\u4e8c\u4e2a\u53c2\u6570\u66ff\u6362{1}\u7b49\u7b49\u3002<\/p>\n<p><pre lang=\"javascript\">\nString.implement({\n    format: function() {\n        \/\/\/&lt;summary&gt;\n        \/\/\/ \u683c\u5f0f\u5316\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u66ff\u6362\u5176\u4e2d\u7528\u5927\u62ec\u53f7\u5305\u542b\u7684\u6570\u5b57\u53ca\u5927\u62ec\u53f7\uff0c\u7c7b\u4f3cC#\u7684format\n        \/\/\/ \u5982\u7528\u7b2c\u4e00\u4e2a\u53c2\u6570\u66ff\u6362{0}\n        \/\/\/&lt;\/summary&gt;\n        \/\/\/&lt;param name=&quot;values...&quot; type=&quot;String&quot;&gt;\u76f8\u5173\u53c2\u6570&lt;\/param&gt;\n        \/\/\/&lt;returns type=&quot;STRING&quot; \/&gt;\n        var s = this;\n        for (var i = 0; i &lt; arguments.length; i++) {\n            s = s.replace(new RegExp(&quot;\\\\{&quot; + i + &quot;\\\\}&quot;, &quot;g&quot;), arguments[i]);\n        }\n        return s;\n    }\n}\n<\/pre>\n<\/p>\n<h4>\u7b80\u5355\u793a\u4f8b\uff1a<\/h4>\n<p><pre lang=\"javascript\">\n\/\/ \u4e0b\u9762\u5c06\u8fd4\u56de 'my name is Fdream, I'm a boy'\nvar a = 'my name is {0}, I'm a {1}'.format('Fdream', 'boy');\n<\/pre>\n<\/p>\n<h3>\u65b9\u6cd5\uff1aformatBy<\/h3>\n<p>\u8bf4\u660e\uff1a\u597d\uff0c\u8fd9\u4e2a\u662f\u4e00\u4e2a\u590d\u6742\u800c\u5f3a\u5927\u7684format\u65b9\u6cd5\uff0c\u51e0\u4e4e\u53ef\u4ee5\u968f\u5fc3\u6240\u6b32\u5730\u5bf9\u4f60\u7684\u5b57\u7b26\u4e32\u8fdb\u884c\u683c\u5f0f\u5316\uff0c\u7528\u6a21\u677f\u5904\u7406\u65f6\u975e\u5e38\u65b9\u4fbf\u3002\u4e0d\u8fc7\u6ce8\u610f\u8fd9\u91cc\u7684\u683c\u5f0f\u7a0d\u5fae\u53d8\u5316\u4e86\u4e00\u4e0b\uff0c\u662f\u66ff\u6362\u7c7b\u4f3c${abc}\u8fd9\u6837\u7684\u5b57\u7b26\u4e32\u3002\u5176\u4e2d\uff0cabc\u662f\u53c2\u6570\u5bf9\u8c61\u7684\u4e00\u4e2a\u5c5e\u6027\uff0c${abc}\u8bb2\u88ab\u53c2\u6570\u4f20\u8fdb\u6765\u7684\u5bf9\u8c61\u7684abc\u5c5e\u6027\u7684\u503c\u7ed9\u66ff\u6362\u6389\u3002\u5728\u8fd9\u4e2a\u91cc\u9762\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u5206\u9694\u7b26\uff0c\u4f60\u53ef\u4ee5\u5bf9\u8fd9\u4e2a\u5c5e\u6027\u503c\u8fdb\u884c\u51fd\u6570\u5904\u7406\u6216\u8005\u622a\u53d6\u3002\u5177\u4f53\u8bf7\u53c2\u8003\u540e\u9762\u7684\u793a\u4f8b\u3002<\/p>\n<p><pre lang=\"javascript\">\nString.implement({\n    formatBy: function(obj, funs) {\n        \/\/\/&lt;summary&gt;\n        \/\/\/ \u7528\u5bf9\u8c61\u683c\u5f0f\u5316\u4e00\u4e2a\u5b57\u7b26\u4e32\n        \/\/\/ \u53ef\u4ee5\u5bf9\u683c\u5f0f\u5316\u7684\u5b57\u7b26\u4e2d\u8fdb\u884c\u6307\u5b9a\u7684\u51fd\u6570\u66ff\u6362\u6216\u8005\u622a\u53d6\n        \/\/\/&lt;\/summary&gt;\n        \/\/\/&lt;param name=&quot;obj&quot; type=&quot;object&quot;&gt;\u7528\u6765\u66ff\u6362\u683c\u5f0f\u7684\u5bf9\u8c61&lt;\/param&gt;\n        \/\/\/&lt;param name=&quot;funs&quot; type=&quot;object&quot;&gt;\u76f8\u5e94\u7684\u66ff\u6362\u51fd\u6570&lt;\/param&gt;\n        \/\/\/&lt;returns type=&quot;STRING&quot; \/&gt;\n        funs = funs || {};\n        return this.replace(\/\\$\\{([^\\}]+)\\}\/g, function(a, b) {\n            var c = b.split(':');\n            var d = c[0].split('|');\n            if (d.length == 1) {\n                if (d[0] in obj) return c[1] ? obj[d[0]].truncate(c[1].toInt()) : obj[d[0]];\n            } else {\n                var f = d[1].trim();\n                if ((f in funs) &amp;&amp; (d[0] in obj)) {\n                    var r = funs[f](obj[d[0]]);\n                    return c[1] ? r.truncate(c[1].toInt()) : r;\n                }\n            }\n            return a;\n        })\n    }\n}\n<\/pre>\n<\/p>\n<h4>\u7b80\u5355\u793a\u4f8b\uff1a<\/h4>\n<p>\u5148\u6765\u4e00\u4e2a\u7b80\u5355\u7684\u7528\u6cd5\uff0c\u548c\u4e0a\u9762\u7684format\u7c7b\u4f3c\uff1a<\/p>\n<p><pre lang=\"javascript\">\nvar obj = { 'name': 'PPLive', 'age': '4' };\nvar fmt = '${name} is ${age} years old now!';\n\/\/ \u73b0\u5728fmt\u5c31\u7b49\u4e8e 'PPLive is 4 years old now!'\n<\/pre>\n<\/p>\n<p>\u518d\u6765\u4e00\u4e2a\u590d\u6742\u4e00\u70b9\u7684\u5e94\u7528\uff1a<\/p>\n<p><pre lang=\"javascript\">\nvar funs = {\n    sexFun: function(sex) {\n        switch (sex) {\n            case 1:\n            case '1':\n                return 'boy';\n            default:\n                return 'girl';\n        }\n    }\n};\nvar obj = { 'name': 'Tom', 'sex': '1', 'address': 'Zhangjiang High Tech Park' };\nvar fmt = '${name} is a ${sex|sexFun}, address is ${address:14}';\n\/\/ \u73b0\u5728fmt\u5c31\u7b49\u4e8e 'Tom is a boy, address is Zhangjiang Hig'\n<\/pre>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ee7\u7eedMootools\u5e38\u7528\u65b9\u6cd5\u6269\u5c55\uff0c\u4f9d\u7136\u8fd8\u662fString\u7c7b\u7684\u6269\u5c55\u3002 \u65b9\u6cd5\uff1aformat \u8bf4\u660e\uff1a\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684format\u65b9\u6cd5\uff0c\u548cC#\u91cc\u9762\u7684format\u7c7b\u4f3c\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u7528\u53c2\u6570\u6765\u4f9d\u6b21\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u7528\u5927\u62ec\u53f7{}\u62ec\u8d77\u6765\u7684\u6570\u5b57\uff0c\u6bd4\u5982\u7528\u7b2c\u4e00\u4e2a\u53c2\u6570\u66ff\u6362{0}\uff0c\u7528\u7b2c\u4e8c\u4e2a\u53c2\u6570\u66ff\u6362{1}\u7b49\u7b49\u3002 String.implement({ format: function() { \/\/\/&lt;summary&gt; \/\/\/ \u683c\u5f0f\u5316\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u66ff\u6362\u5176\u4e2d\u7528\u5927\u62ec\u53f7\u5305\u542b\u7684\u6570\u5b57\u53ca\u5927\u62ec\u53f7\uff0c\u7c7b\u4f3cC#\u7684format \/\/\/ \u5982\u7528\u7b2c\u4e00\u4e2a\u53c2\u6570\u66ff\u6362{0} \/\/\/&lt;\/summary&gt; \/\/\/&lt;param name=&quot;values&#8230;&quot; type=&quot;String&quot;&gt;\u76f8\u5173\u53c2\u6570&lt;\/param&gt; \/\/\/&lt;returns type=&quot;STRING&quot; \/&gt; var s = this; for (var i = 0; i &lt; arguments.length; i++) { s = s.replace(new RegExp(&quot;\\\\{&quot; + i + &quot;\\\\}&quot;, &quot;g&quot;), arguments[i]); } return s; } } \u7b80\u5355\u793a\u4f8b\uff1a \/\/ \u4e0b\u9762\u5c06\u8fd4\u56de &#8216;my name is Fdream, &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[120,104,328],"class_list":["post-651","post","type-post","status-publish","format-standard","hentry","category-coding","tag-AJAX","tag-JavaScript","tag-mootools"],"views":5278,"_links":{"self":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts\/651","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/comments?post=651"}],"version-history":[{"count":0,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts\/651\/revisions"}],"wp:attachment":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/media?parent=651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/categories?post=651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/tags?post=651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}