µ±Ç°Ò³Ãæ: ¿ª·¢×ÊÁÏÊ×Ò³ ¡ú Javascript רÌâ ¡ú ÓÃjavascriptÄ£ÄâC#µÄ[Attribute]Ó÷¨
ÓÃjavascriptÄ£ÄâC#µÄ[Attribute]Ó÷¨
ÕªÒª: ÓÃjavascriptÄ£ÄâC#µÄ[Attribute]Ó
<textarea readonly style="border:none;font-family:Courier New;line-height:150%;width:760px;overflow-y:visible">
¡¡¡¡Ö´Ðнá¹û£º
¡¡¡¡<textarea rows = "10" cols = "100" id = "output"></textarea>
<¡¡¡¡
br />
¡¡¡¡µ÷ÊÔÐÅÏ¢£º
<¡¡¡¡<textarea rows = "10" cols = "100" id = "debug"></textarea>
div style="font-size: 14px; line-height: 25px;">
¡¡¡¡
¡¡¡¡<script language="javascript">
¡¡¡¡/* ÌØÐÔ£¨Attributes£©ÊÇÒ»ÖÖոеÄÉùÃ÷ÐÔÐÅÏ¢¡£
¡¡¡¡ÎÒÃDz»½ö¿ÉÒÔͨ¹ýÌØÐÔÀ´¶¨ÒåÉè¼Æ²ãÃæµÄÐÅÏ¢
¡¡¡¡£¨ÀýÈçhelp file, URL for documentation£©
¡¡¡¡ÒÔ¼°ÔËÐÐʱ£¨run-time£©ÐÅÏ¢£¨ÀýÈçʹXMLÓëclassÏàÁªÏµ£©£¬
¡¡¡¡¶øÇÒÎÒÃÇ»¹¿ÉÒÔÀûÓÃÌØÐÔ½¨Á¢×ÔÃèÊö£¨self-describing£©×é¼þ¡£
¡¡¡¡*/
¡¡¡¡
¡¡¡¡function Attribute()¡¡//Attribute »ùÀà,¿ÉÒÔ×ÔÐж¨ÒåÆäÖеĽӿÚÒÔÀ©³ä¹¦ÄÜ£¬ÕâÀïÖ»ÊÇÒ»¸ö¼òµ¥µÄÑÝʾ£¬Òò´ËÁô¿Õ
¡¡¡¡{
¡¡¡¡
¡¡¡¡}
¡¡¡¡
¡¡¡¡function TestMethod() //¶¨ÒåÒ»¸öеÄAttributeÀà TestMethod£¬ÓÃËüÀ´¸øÐèÒª½øÐе¥Ôª²âÊԵķ½·¨Ìṩ¶îÍâÐÅÏ¢
¡¡¡¡{
¡¡¡¡this.name = "TestMethod";
¡¡¡¡}TestMethod.prototype = new Attribute();
¡¡¡¡function TestMethodAttribute() //±ØÐèµÄÖ´Ðз½·¨
¡¡¡¡{
¡¡¡¡return new TestMethod();
¡¡¡¡}
¡¡¡¡
¡¡¡¡function DebugOutput(bOutput) //¶¨ÒåÒ»¸öеÄAttributeÀà DebugOutput£¬ÓÃËüÀ´Ö¸Ê¾ÊÇ·ñÔÚ²âÊÔÖÐÊä³ö¶îÍâµÄµ÷ÊÔÐÅÏ¢
¡¡¡¡{
¡¡¡¡this.name = "DebugOutput";
¡¡¡¡this.isAllowDebugOutput = bOutput;
¡¡¡¡}DebugOutput.prototype = new Attribute();
¡¡¡¡function DebugOutputAttribute(bOutput)¡¡//±ØÐèµÄÖ´Ðз½·¨
¡¡¡¡{
¡¡¡¡return new DebugOutput(bOutput);
¡¡¡¡}
¡¡¡¡
¡¡¡¡Function.__captureAttributes = function(obj)
¡¡¡¡{
¡¡¡¡var attributeDef = /\[\w+\].*\n.*(?=\=[\s]*function)/g;
¡¡¡¡var matches = obj.constructor.toString().match(attributeDef);
¡¡¡¡if(matches != null)
¡¡¡¡{
¡¡¡¡for (var i = 0; i < matches.length; i++)
¡¡¡¡{
¡¡¡¡var part = matches[i].split(/[\s\n]/);
¡¡¡¡var attrLists = part[0].split(",");
¡¡¡¡var methodObj = eval(part[part.length-1]);
¡¡¡¡methodObj.__attributes = new Array();
¡¡¡¡methodObj.__attributes.__all = new Array();
¡¡¡¡
¡¡¡¡for (var j = 0; j < attrLists.length; j++)
¡¡¡¡{
¡¡¡¡if(!/^.+\(.*\)$/.test(attrLists[j].slice(1,-1)))
¡¡¡¡{
¡¡¡¡attrLists[j] = "[" + attrLists[j].slice(1,-1) + "()" + "]"; //´¦ÀíÊ¡ÂÔÀ¨ºÅµÄÇé¿ö
¡¡¡¡}
¡¡¡¡if(!/^.+Attribute$/.test(attrLists[j].split("(")[0]))
¡¡¡¡{
¡¡¡¡attrLists[j] = attrLists[j].split("(")[0] + "Attribute" + "(" + attrLists[j].split("(")[1];
¡¡¡¡}
¡¡¡¡
¡¡¡¡var attrObj = eval(eval(attrLists[j])[0]);
¡¡¡¡methodObj.__attributes.__all.push(attrObj);
¡¡¡¡methodObj.__attributes[attrLists[j].split("(")[0].replace(/[\[\]]/g,"").replace(/Attribute$/g,"")] = attrObj;
¡¡¡¡methodObj.__attributes[attrLists[j].split("(")[0].replace(/[\[\]]/g,"")] = attrObj;
¡¡¡¡}
¡¡¡¡}
¡¡¡¡}
¡¡¡¡}
¡¡¡¡
¡¡¡¡function UnitTest()¡¡//µ¥Ôª²âÊÔ¿ò¼Ü£¬±»¸³Óè[TestMethod]ÌØÐԵķ½·¨»á±»×÷ΪCaseÖ´ÐвâÊÔ
¡¡¡¡{
¡¡¡¡this.errors = 0;
¡¡¡¡this.passed = 0;
¡¡¡¡//ÉùÃ÷TestMethodÌØÐÔ£¬testString·½·¨½«±»runCase·½·¨Ö´ÐУ¬Í¬Ê±ÉùÃ÷ÁËDebugOutputÌØÐÔ£¬½«·µ»ØµÄÐÅÏ¢Êä³öµ½µ÷ÊÔ´°¿Ú
¡¡¡¡//ÌØÐÔµÄÉùÃ÷±ØÐë·ÅÔÚ±»Ö¸¶¨ÌØÐԵķ½·¨Ö®Ç°£¬¶øÇÒÒª¶ÀÕ¼Ò»ÐУ¬Èç¹ûÓжà¸öÌØÐÔ¿ÉÒÔÒÔ¶ººÅ·Ö¸ô
¡¡¡¡//°üº¬ÌØÐÔÉùÃ÷µÄº¯ÊýÒªÒÔ";"½á⣬²»¿ÉÊ¡ÂÔ
¡¡¡¡[TestMethod],[DebugOutput(true)]
¡¡¡¡UnitTest.prototype.testString = function()¡¡//²âÊÔ×Ö·û´®·½·¨£¬ÕâÀï¼ÙÉè×Ô¼ºÊµÏÖÁËÒ»¸öStringÀàÈ»ºóÀ´²âÊÔ
¡¡¡¡{
¡¡¡¡var testCase = new String();
¡¡¡¡testCase = "abc";
¡¡¡¡this.Test(testCase == "abc");¡¡//²âÊÔ¸³Öµ²Ù×÷
¡¡¡¡testCase += "def";
¡¡¡¡this.Test(testCase == "abcdef"); //²âÊÔÁ¬½Ó²Ù×÷
¡¡¡¡this.Test(testCase.length == 6); //²âÊÔ³¤¶ÈÊôÐÔ
¡¡¡¡
¡¡¡¡self.output.value += "\n";
¡¡¡¡var result = "Debug - testString finished with " + this.passed + " cases passed and " + this.errors + " cases failed!\n";
¡¡¡¡this.passed = 0;
¡¡¡¡this.errors = 0;
¡¡¡¡return result;
¡¡¡¡};
¡¡¡¡//Ö»²âÊÔ²»Êä³öµ÷ÊÔÐÅÏ¢µÄ·½·¨
¡¡¡¡[TestMethod]
¡¡¡¡UnitTest.prototype.testRegexp = function()
¡¡¡¡{
¡¡¡¡var errors = 0;
¡¡¡¡var passed = 0;
¡¡¡¡if(/abc/.test("abc"))
¡¡¡¡{
¡¡¡¡self.output.value += ".";
¡¡¡¡passed ++;
¡¡¡¡}
¡¡¡¡else
¡¡¡¡{
¡¡¡¡self.output.value += "e";
¡¡¡¡errors ++;
¡¡¡¡}
¡¡¡¡if(/abc/.test("aababcd"))
¡¡¡¡{
¡¡¡¡self.output.value += ".";
¡¡¡¡passed ++;
¡¡¡¡}
¡¡¡¡};
¡¡¡¡
¡¡¡¡//²»±»²âÊԵķ½·¨
¡¡¡¡UnitTest.prototype.foo = function()
¡¡¡¡{
¡¡¡¡alert('foo not being tested!');
¡¡¡¡};
¡¡¡¡UnitTest.prototype.runCases = function()
¡¡¡¡{
¡¡¡¡for (each in this)
¡¡¡¡{
¡¡¡¡if(this[each].__attributes != null && this[each].__attributes["DebugOutput"] != null)
¡¡¡¡{
¡¡¡¡var result = this[each].call(this);
¡¡¡¡if(this[each].__attributes["DebugOutput"].isAllowDebugOutput)
¡¡¡¡{
¡¡¡¡self.debug.value = result;
¡¡¡¡}
¡¡¡¡}
¡¡¡¡else if(this[each].__attributes != null && this[each].__attributes["TestMethod"] != null)
¡¡¡¡{
¡¡¡¡this[each].call(this);
¡¡¡¡}
¡¡¡¡}
¡¡¡¡};
¡¡¡¡UnitTest.prototype.Test = function(cond)
¡¡¡¡{
¡¡¡¡if(cond)
¡¡¡¡{
¡¡¡¡self.output.value += ".";
¡¡¡¡this.passed ++;
¡¡¡¡}
¡¡¡¡else
¡¡¡¡{
¡¡¡¡self.output.value += ".";
¡¡¡¡this.errors ++;
¡¡¡¡}
¡¡¡¡};
¡¡¡¡//ÔÚÀàÄÚ²¿²¶»ñAttribute¶ÔÏ󣬱ØÐëÔÚʹÓÃÌØÐԵĶÔÏóÄÚ²¿ÉùÃ÷£¬ÕâÒ»µãͬC#»¹ÊÇÓÐÇø±ðµÄ
¡¡¡¡Function.__captureAttributes(this);
¡¡¡¡}
¡¡¡¡
¡¡¡¡var test = new UnitTest();
¡¡¡¡test.runCases();
¡¡¡¡//»òÐíһЩÈ˲»Ì«Ï°¹ßÉÏÃæµÄÕâÖÖ×ö·¨£¬µ«ÊÇËüÓÐÒ»¸öÏÔ¶øÒ×¼ûµÄºÃ´¦¾ÍÊÇÎÒÈç¹ûÏ£ÍûÌí¼Ó¸ü¶àµÄµ¥Ôª²âÊÔÓÃÀý£¬Ö»ÐèÒªÔö¼Óеıê¼ÇΪ[TestMethod]µÄ·½·¨£¬¶ø²»ÓÃÐÞ¸ÄrunCases·½·¨µÄÈκδúÂ룡ÕâÑùÎҾͿÉÒÔ½«Õû¸öµ¥Ôª²âÊÔ¿ò¼Ü¡°·â×°ÆðÀ´¡±¶øÒÀÈ»ÔÊÐíʹÓÃÕß´Ó¡°Íⲿ¡±Ìí¼Ó×Ô¼ºµÄ²âÊÔ·½·¨£¡
¡¡¡¡
¡¡¡¡//³ý´ËÒÔÍ⣬ÎÒÃÇ¿ÉÒÔÓá°ÌØÐÔ¡±Ï൱±ãÀûµØÓÃÀ´ÊµÏÖÐí¶àģʽ£¬Õâ·½ÃæµÄ¾ßÌåÉîÈëÓ÷¨ÕâÀï²»ÔÙÏêÊöÁË£¬ÓÐÐËȤµÄÅóÓÑ¿ÉÒÔ×ÔÐг¢ÊÔ^^£¬²»¹ýÏÖÔÚÕâ¸öÄ£ÄâµÄ¡°ÌØÐÔ¡±»¹ÓÐһЩ²»×ãÖ®´¦£¬ÀýÈçÖ»Äܽ«ÌØÐÔÉùÃ÷µ½¶ÔÏó¡°·½·¨¡±¶ø²»ÄÜÉùÃ÷¸ø¶ÔÏó±¾Éí£¬ÕâÑùҪʵÏÖһЩÏñSerializableÖ®ÀàµÄ¶ÔÏóÌØÐԾͲ»Ì«·½±ãÁË=.=
¡¡¡¡</script> </textarea>
¡ü·µ»ØÄ¿Â¼
ǰһƪ:
VS .net 2003µ÷ÊÔjavascriptÖÐÁ½¸öÔÓÖ¢µÄ½â¾ö
ºóһƪ:
´ó»ÒÀǵÄJavascript¹¤¾ßÏä(¶þ)¡ª¡ªie°´Å¥´ó¼¯ºÏ£¬vbscriptºÍjavascript»¥Ïàµ÷Ó÷½·¨