1

我有这个扩展检测功能,但它严重依赖于大量的 if/then 语句。我可以使用不同的方法来获得相同的结果吗?(所有变量都在更高级别定义)。我想处理更少的代码,并提高性能速度。

if (extT == 'app') { docType = 'app'; docDesc = 'Application'; }
        else if (extT == 'exe') { docType = 'exe'; docDesc = 'Executable File'; }
        else if (extT == 'vwx') { docType = 'vwx'; docDesc = 'Vectorworks File'; }
        else if (extT == 'torrent') { docType = 'torrent'; docDesc = 'BitTorrent Document'; }
        else if (extT == 'ipsw') { docType = 'ipsw'; docDesc = 'Apple Device Software Update File'; }
        else if (extT == 'mpkg') { docType = 'mpkg'; docDesc = 'Installer package'; }
        else if (extT == 'prefPane') { docType = 'prefPane'; docDesc = 'Mac OS X Preference Pane'; }
        else if (extT == 'ogg') { docType = 'ogg_audio'; docDesc = 'Ogg Vorbis'; }
        else if (extT == 'ogv') { docType = 'ogv'; docDesc = 'Ogg Vorbis'; }
        else if (extT == 'flac') { docType = 'flac_audio'; docDesc = 'FLAC Audio File'; }
        else if (extT == 'ttf') { docType = 'ttf'; docDesc = 'TrueType Font'; }
        else if (extT == 'otf') { docType = 'otf'; docDesc = 'OpenType Font'; }
        else if (extT == 'air') { docType = 'air'; docDesc = 'Rich Internet Application'; }
        else if (extT == 'csproj') { docType = 'csproj'; docDesc = '.NET Source Code'; }
        else if (extT == 'download') { docType = 'download'; docDesc = 'File Download'; }
        else if (extT == 'log') { docType = 'log'; docDesc = 'Log File'; }
        else if (extT == 'conf') { docType = 'conf'; docDesc = 'IRC Related Configuration File'; }
        else if (extT == 'command') { docType = 'command'; docDesc = 'Terminal Shell Script'; }
        else if (extT == 'plist') { docType = 'plist'; docDesc = 'Property List XML File'; }
        else if (extT == 'mobileprovision') { docType = 'Developer Provisioning Profile'; docDesc = ''; }
        else if (extT == 'ds_store') { docType = 'DS_Store'; docDesc = 'Desktop Services Store'; }
        else if (extT == 'asp') { docType = 'asp'; docDesc = 'Active Server Pages'; }
        else if (extT == 'safariextz') { docType = 'safariextz'; docDesc = 'Safari Extension'; }
        else if (extT == 'qlab2license') { docType = 'QLab2License'; docDesc = 'QLab License File'; }
        else if (extT == 'qlablicense') { docType = 'QLabLicense'; docDesc = 'QLab License File'; }
        else if (extT == 'qlab1license') { docType = 'QLab1License'; docDesc = 'QLab License File'; }
        else if (extT == 'qlab3license') { docType = 'QLab3License'; docDesc = 'QLab License File'; }
        else if (extT == 'saver') { docType = 'saver'; docDesc = 'Mac OS X Preference Screen Saver'; }
        else if (extT == 'pkg') { docType = 'pkg'; docDesc = 'Installer Package'; }
        else if (extT == 'w3x') { docType = 'w3x'; docDesc = 'Warcraft III: The Frozen Throne'; }
        else if (extT == 'fcp') { docType = 'fcp'; docDesc = 'Final Cut Project File'; }
        else if (extT == 'pages') { docType = 'pages'; docDesc = 'Pages Publication'; }
        else if (extT == 'numbers') { docType = 'numbers'; docDesc = 'Numbers Document'; }
        else if (extT == 'key') { docType = 'key'; docDesc = 'Keynote Document'; }
        else if (extT == 'wwf') { docType = 'wwf'; docDesc = 'WWF Non-printable Document'; }
        else if (extT == 'swd') { docType = 'swd'; docDesc = 'Shockwave Document'; }
        else if (extT == 'rtfd') { docType = 'rtfd'; docDesc = 'NeXT RTF-Packet'; }
        else if (extT == 'wtpl') { docType = 'wtpl'; docDesc = 'WireTap Studio Library'; }
        else if (extT == 'abbu') { docType = 'abbu'; docDesc = 'Address Book Archive File'; }
        else if (extT == 'thllibrary') { docType = 'thllibrary'; docDesc = 'The Hit List Library'; }
        else if (extT == 'logic') { docType = 'logic'; docDesc = 'Logic Project File'; }
        else if (extT == 'pat') { docType = 'pat'; docDesc = 'Adobe Photoshop Patterns File'; }
        else if (extT == 'dwg') { docType = 'dwg'; docDesc = 'AutoCAD Image'; }
        else if (extT == 'ctb') { docType = 'ctb'; docDesc = 'AutoCAD Color-Based Plot Style File'; }
        else if (extT == 'stb') { docType = 'stb'; docDesc = 'AutoCAD Plot Style Table File'; }
        else if (extT == 'lck') { docType = 'lck'; docDesc = 'Lock File'; }
        else if (extT == 'aep') { docType = 'aep'; docDesc = 'After Effects Project'; }
        else if (extT == 'prtl') { docType = 'prtl'; docDesc = 'Adobe Permiere Title File'; }
        else if (extT == 'prproj') { docType = 'prproj'; docDesc = 'Adobe Premiere Project'; }
        else if (extT == 'band') { docType = 'band'; docDesc = 'GarageBand Project File'; }
        else if (extT == 'h') { docType = 'h'; docDesc = 'Header File'; }
        else if (extT == 'c') { docType = 'c'; docDesc = 'C/C++ Program File'; }
        else if (extT == 'r') { docType = 'r'; docDesc = 'Rez Source Code File'; }
        else if (extT == 'xcodeproj') { docType = 'xcodeproj'; docDesc = 'XCode Project'; }
        else if (extT == 'xcconfig') { docType = 'xcconfig'; docDesc = 'Xcode Configuration Settings'; }
        else if (extT == 'rcproject') { docType = 'rcproject'; docDesc = 'iMovie Project'; }
        else if (extT == 'cache') { docType = 'cache'; docDesc = 'Cache File'; }
        else if (extT == 'exs') { docType = 'exs'; docDesc = 'EXS24 instrument file'; }
        else if (extT == 'sdir') { docType = 'SDIR'; docDesc = 'Space Designer Impulse Response'; }
        else if (extT == 'ovw') { docType = 'ovw'; docDesc = 'Cool Edit Pro Overview File'; }
        else if (extT == 'lwp') { docType = 'lwp'; docDesc = 'LightWright Project'; }
        else if (extT == 'bin') { docType = 'BIN'; docDesc = 'Binary File'; }
        else if (extT == 'ini') { docType = 'INI'; docDesc = 'Initialization/Configuration File'; }
        else if (extT == 'ex_') { docType = 'EX_'; docDesc = 'Compressed EXE File'; }
        else if (extT == 'bat') { docType = 'bat'; docDesc = 'Microsoft Batch File'; }
        else if (extT == 'edml') { docType = 'edml'; docDesc = 'Adobe Extension Data Markup Language (EDML) Document'; }
        else if (extT == 'mak') { docType = 'MAK'; docDesc = 'MakeFile'; }
        else if (extT == 'cfm') { docType = 'cfm'; docDesc = 'Adobe ColdFusion Document'; }
        else if (extT == 'indd') { docType = 'indd'; docDesc = 'Adobe InDesign Document'; }
        else if (extT == 'indt') { docType = 'indt'; docDesc = 'Adobe InDesign Template'; }
        else if (extT == 'asnd') { docType = 'asnd'; docDesc = 'Adobe Sound Document'; }
        else if (extT == 'cpp') { docType = 'cpp'; docDesc = 'C++ Source'; }
        else if (extT == 'aac') { docType = 'aac'; docDesc = 'MPEG-2 Advanced Audio Coding File'; }
        else if (extT == 'm2v') { docType = 'm2v'; docDesc = 'MPEG-2 Blu-ray File'; }
        else if (extT == 'olproj') { docType = 'olproj'; docDesc = 'Adobe OnLocation Project'; }
        else if (extT == 'ipa') { docType = 'ipa'; docDesc = 'iOS Application'; }
        else if (extT == 'apk') { docType = 'apk'; docDesc = 'Android Application'; }
        else if (extT == 'scpt') { docType = 'scpt'; docDesc = 'Script File'; }
        else if (extT == 'applescript') { docType = 'AppleScript'; docDesc = 'AppleScript File'; }
        else if (extT == 'emlxpart') { docType = 'emlxpart'; docDesc = 'Mail Message Attachment'; }
        else if (extT == 'emlx') { docType = 'emlx'; docDesc = 'Mail Message'; }
        else if (extT == 'webbookmark') { docType = 'webbookmark'; docDesc = 'Safari Bookmark'; }
        else if (extT == 'jsx') { docType = 'jsx'; docDesc = 'Adobe JavaScript File'; }
        else if (extT == 'icns') { docType = 'icns'; docDesc = 'Apple Icon Image'; }
        else if (extT == 'caf') { docType = 'caf_audio'; docDesc = 'Apple Loop'; }
        else if (extT == 'cues') { docType = 'cues'; docDesc = 'QLab Workspace'; }
        else { docType = '--'; }
4

3 回答 3

11

将对象用作地图。

var map = {
    "exe": {type:"exe",desc:"Executable File"},

    "vwx": {type:"vwx",desc:"Vectorworks File"}
};

var extT = "exe";

var docType = map[extT].type;
var docDesc = map[extT].desc;

http://jsfiddle.net/ZfbHf/1/

优点

  • 将数据列为实际数据,然后仅包含 2 行赋值
  • 可以从 JSON 文件中加载数据,将其从代码中完全删除并使其可配置而无需更改代码
  • 清晰的数据结构
  • 易于遵循的清晰逻辑(以后不会有人在其中一个 if 语句中添加随机代码行使其难以调试)
于 2013-08-09T04:04:53.387 回答
2

它看起来不会像更少的代码,但是如果您使用对象来创建地图,它应该会更高效:

var types = {
    'app': { 'docType': 'app', 'docDesc': 'Application' }, 
    'exe': { 'docType': 'exe', 'docDesc': 'Executable File' }, 
    ...
};

然后将其用作查找:

var curType = types['app'];

基本上所有@ben336 所说的 - 使用他的答案

于 2013-08-09T04:06:06.620 回答
0

可以使用 case/switch 语句,但实际上您将处于同一条船上,并且(据我所知)两者之间实际上没有性能差异。再说一次,我的最后一句话是基于我的 C# 知识。也许将此信息预加载到某种数组中,从数据库中提取它或其他什么?这样你就可以根据你传递给它的参数来调用它,然后简单地返回你需要的值。

目前尚不清楚您使用的是什么平台而不是 javascript,因此在给出答案时真的不能走得太远。我的 2 美分至少值....

注意 查看 Ben336 和 dc5 的答案,这看起来也是可行的选择。

于 2013-08-09T04:06:57.547 回答