Closure编译器不重命名属性和方法(高级编译)

时间:2013-08-01 11:58:47

标签: javascript google-closure-compiler

为什么Closure Compiler不重命名我的pathstart等属性,但在使用高级编译时它会重命名我的limit属性?

我希望它重命名我的代码中的每个属性和方法,但导出的构造函数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
})();

1 个答案:

答案 0 :(得分:0)

除非使用aliasExternals,否则不会重命名(默认)externs中定义的属性名称。

所以sort,getElementById,parseInt和很多其他人都不会被重命名。我想编译器应该将您的属性视为RFM的属性,但似乎它没有。如果重命名myPath的路径,则应该重命名它,或者在编译时使用aliasExternals。