1. 程式人生 > >js中eval的使用可以達到java反射功能

js中eval的使用可以達到java反射功能

javascript中eval的使用可以達到java反射的效果

<%
/**********************************************************************
* 工作流附件管理js
* 2016-4-8 許暢 新建
**********************************************************************/
%>
<!DOCTYPE html>
<head>
   <script src='/xxx/js/jquery-1.5.2.min.js'></script>
   
   <script type="text/javascript">
     var webRoot= "<%=pageContext.getAttribute("cuiWebRoot") %>";
    
     $(function() {
    	 //jquery 選擇器
         var selector = "attach";
         //附件元素id
         var attachId = $(selector).attr("id");
         
         var attach = new Attachment();
         //附件屬性值陣列
         var keys = ["id", "frameHeight", "jobTypeCode", "objId", "displayMode", "operateMode", "relateMode",
             "operationRight", "title", "hiddenId", "hrefName", "creatorId", "creatorName", "afterUpload", "afterDelete",
             "afterEditFileName", "isNeeded", "isShowAttachmentType", "showFields", "showExtendAttribute", "icon", "extendClass",
             "extendParam", "dwrUrl", "readOnly", "returnURL", "queryClass", "extendClassify",
             "objectIdList", "attachmentTypeIdList", "attachmentTypeControl"
         ];

         for (var i in keys) {
             var key = keys[i];
             var attr = $(selector).attr(key);
             if (!attr){
                continue;
             }
             var func = "attach.set" + replaceFirstUper(key) + "('"+attr+"');";
             eval(func);
         }
         //生成附件
         attach.setWebRoot(webRoot);
         attach.init(attachId);
     });

     //正則將首字元替換為大寫
     var replaceFirstUper = function(str) {
        if(str && str.length>0){
            return str.substring(0,1).toUpperCase()+str.substring(1,str.length);
        }
        return str;
     };
   </script>
</head>