2013-08-01 34 views
1

爲什麼Closure編譯器不會重命名我的path,start等屬性,但在使用高級編譯時它會重命名我的limit屬性?Closure編譯器不重命名屬性和方法(高級編譯)

我希望它重新命名我的代碼中的每個屬性和方法,除了導出的構造函數window.RFM

這裏的構造函數片段:

var RFM = function(node) { 
    ... 
    this.path = '/'; 
    this.limit = 10; 
    ... 
}; 

它得到編譯:

... 
    this.path = '/'; 
    this.c = 10; 
    ... 

我曾嘗試添加註釋像@private@constructor,但它沒有任何效果。

我已經在http://closure-compiler.appspot.com/

這裏測試的是全碼:

(function() { 
var ajax = {}; 
ajax.x = function() { 
    try { 
     return new ActiveXObject('Msxml2.XMLHTTP') 
    } catch (e1) { 
     try { 
      return new ActiveXObject('Microsoft.XMLHTTP') 
     } catch (e2) { 
      return new XMLHttpRequest() 
     } 
    } 
}; 

ajax.send = function(url, callback, method, data, sync) { 
    var x = ajax.x(); 
    x.open(method, url, sync); 
    x.onreadystatechange = function() { 
     if (x.readyState == 4) { 
      callback(x.responseText) 
     } 
    }; 
    if (method == 'POST') { 
     x.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); 
    } 
    x.send(data) 
}; 

ajax.get = function(url, data, callback, sync) { 
    var query = []; 
    for (var key in data) { 
     query.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key])); 
    } 
    ajax.send(url + '?' + query.join('&'), callback, 'GET', null, sync) 
}; 

ajax.post = function(url, data, callback, sync) { 
    ajax.send(url, callback, 'POST', data, sync) 
}; 


var RFM = function(node) { 
    this.node = node; 
    node.innerHTML = this.template(RFM.templates.dialog); 

    this.nodeFiles = node.getElementsByClassName('rfm-files')[0]; 
    this.nodeDirectories = node.getElementsByClassName('rfm-directories')[0]; 
    this.nodeTags = node.getElementsByClassName('rfm-tags')[0]; 

    this.nodeDirectories.addEventListener('click', this.clickedDirectory.bind(this)); 

    this.path = '/'; 
    this.start = 0; 
    this.limit = 10; 
    this.sort = 'mtime'; 
    this.direction = 'desc'; 
    this.src = 'example.php'; 

    this.refresh(); 
}; 

RFM.prototype.template = function(string, variables) { 
    return string.replace(/\{(.*?)\}/g, function(match, variable) { 
     return variables[variable]; 
    }).replace(/_(.*?)_/g, function(match, variable) { 
     return locale[variable]; 
    }); 
}; 

// Refresh 
RFM.prototype.refresh = function() { 
    ajax.get(this.src, { 
     path: this.path, 
     start: this.start, 
     limit: this.limit, 
     sort: this.sort, 
     direction: this.direction 
    }, function(data) { 
     data = JSON.parse(data); 
     this.refreshFiles(data.files); 
     this.refreshDirectories(data.directories); 
    }.bind(this), false); 
}; 

RFM.prototype.refreshFiles = function(files) { 
    var result = ''; 
    for (var i = 0; i < files.length; i++) { 
     files[i].type = files[i].name.replace(/^.*\./, ''); 
     files[i].mtime = new Date(files[i].mtime * 1000).toISOString().replace('T', ' ').replace(/.{5}$/, ''); 
     result += this.template(RFM.templates.file, files[i]); 
    } 
    this.nodeFiles.innerHTML = result; 
}; 

RFM.prototype.refreshDirectories = function(directories) { 
    var result = ''; 
    if (this.path != '/') { 
     result += this.template(RFM.templates.directory, { 
      name: '..' 
     }); 
    } 
    for (var i = 0; i < directories.length; i++) { 
     result += this.template(RFM.templates.directory, { 
      name: directories[i] 
     }); 
    } 
    this.nodeDirectories.innerHTML = result; 
}; 

// Events 
RFM.prototype.clickedDirectory = function(e) { 
    if (e.target.innerText === '..') { 
     this.path = this.path.replace(/\/[^\/]+\/$/, '/'); 
    } else { 
     this.path += e.target.innerText + '/'; 
    } 
    this.refresh(); 
}; 

var locale = { 
    headingDirectories: 'Directories', 
    headingTags: 'Tags', 
    headingUpload: 'Upload', 
    headingFiles: 'Files', 
    fileName: 'Name', 
    fileSize: 'Size', 
    fileType: 'Type', 
    fileModificationTime: 'Modified' 
}; 

RFM.templates = { 
    "dialog": "<div class=\"rfm-dialog\"> <div class=\"rfm-wrapper\"> <div class=\"rfm-sidebar\"> <div class=\"rfm-directories-wrapper\"> <span class=\"rfm-heading\">_headingDirectories_<\/span> <div class=\"rfm-directories\"><\/div> <\/div> <div class=\"rfm-tag-wrapper\"> <span class=\"rfm-heading\">_headingTags_<\/span> <div class=\"rfm-tags\"><\/div> <\/div> <\/div> <div class=\"rfm-main\"> <div class=\"rfm-upload\"> <span class=\"rfm-heading\">_headingUpload_<\/span> <\/div> <div class=\"rfm-files-wrapper\"> <span class=\"rfm-heading\">_headingFiles_<\/span> <table class=\"rfm-table\"> <thead> <tr> <th><\/th> <th class=\"rfm-file-name\">_fileName_<\/th> <th class=\"rfm-file-type\">_fileType_<\/th> <th class=\"rfm-file-size\">_fileSize_<\/th> <th class=\"rfm-file-mtime\">_fileModificationTime_<\/th> <\/tr> <\/thead> <tbody class=\"rfm-files\"> <\/tbody> <\/table> <\/div> <\/div> <\/div> <div>", 
    "directory": "<div class=\"rfm-directory\">{name}<\/div>", 
    "file": "<tr class=\"rfm-file\"> <td><\/td> <td class=\"rfm-file-name\">{name}<\/td> <td class=\"rfm-file-type\">{type}<\/td> <td class=\"rfm-file-size\">{size}<\/td> <td class=\"rfm-file-mtime\">{mtime}<\/td> <\/tr>" 
}; 
window['RFM'] = RFM; 
})(); 

而且編譯:中(默認)實習醫生中定義的

(function() { 
    function b(a) { 
    a.innerHTML = this.a(b.b.k); 
    this.h = a.getElementsByClassName("rfm-files")[0]; 
    this.d = a.getElementsByClassName("rfm-directories")[0]; 
    this.d.addEventListener("click", this.e.bind(this)); 
    this.path = "/"; 
    this.start = 0; 
    this.c = 10; 
    this.sort = "mtime"; 
    this.direction = "desc"; 
    this.src = "example.php"; 
    this.refresh() 
    } 
    ... 
    window.RFM = b 
})(); 
+0

[爲什麼Closure編譯器不重命名具有特定名稱的對象?](http://stackoverflow.com/questions/12671041/why-does-closure-compiler-not-rename-objects-with-certain-名稱) –

回答

0

屬性名稱將除非使用aliasExternals,否則不會重命名。

所以sort,getElementById,parseInt和很多其他的都不會被重命名。我想編譯器應該看到你的屬性作爲RFM的屬性,但它似乎沒有。如果將路徑重命名爲myPath,則應在重新編譯時重命名或使用aliasExternals。

+2

這不太正確。除非基於類型的優化處於啓用狀態(默認情況下處於關閉狀態),否則將不會重命名externs中定義的屬性名稱。請參閱https://code.google.com/p/closure-compiler/wiki/FAQ#Some_of_my_properties_are_getting_renamed,_but_some_aren't。 –

相關問題