首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >指导手动解除变量和函数的工具?

指导手动解除变量和函数的工具?
EN

Stack Overflow用户
提问于 2017-03-07 23:50:44
回答 1查看 87关注 0票数 0

我有一个缩小的javascript文件。我可以通过各种工具发送它来插入换行符和缩进。然后我想要的是修复变量名。我知道没有任何工具能自动做到这一点。我需要的是一个工具,将加强我的尝试这样做手动。它需要知道作用域规则,以便当我将c重命名为length,将d重命名为height,将j()重命名为move()时,所有地方都会进行这些更改,即使用相同的 cdj,但在存在相同名称的不同变量和函数的其他作用域中则不使用。

是否有这样的工具,专门为逆转小型化而设计的?如果没有用于此特定作业的工具,那么是否有智能IDE至少可以处理按照范围规则重命名变量或方法?

EN

回答 1

Stack Overflow用户

发布于 2017-03-08 05:11:57

我找到了一个浏览器内/可下载的node.js库工具,它可以很好地重命名重构。埃斯皮尔马处理以下ES6代码(从示例中略为修改),以便当我更改任何全局作用域hi的名称时,只会更改由注释块包围的hi名称(我想不出更好的方法来调用代码,因为代码块中没有显示标记)。

代码语言:javascript
复制
// Array shuffling code from Underscore.js, code modified from base example on http://esprima.org/demo/rename.html
var shuffled;

var /*hi*/ = 'hi'; // initial declaration

function /*hi*/() { // modifies var above
    this.shuffle = 'x';
}

_.shuffle = function(obj) {
    function hi() {
        return 'hello';
    }
    var shuffled = [], rand;
    each(obj, function(value, index, list) {
        rand = Math.floor(Math.random() * (index + 1));
        shuffled[index] = shuffled[rand];
        shuffled[rand] = value;
    });
    hi(); // calls function defined above, name not changed by change to global scope 'hi'
    console.log(hello); // considered to be the same as the let statement below even though this is a syntax error since let doesn't get hoisted like var
    return shuffled;
};

let hello = 'hello';

function hiNotInScope() {
    var hi = 'something else'; // not in global scope, so change to global hi doesn't change this
    console.log(hi); // changed if the hi in this scope is changed
}

// hi (not changed since it's in a comment)

/*hi*/(); // calls global hi function.

它似乎尊重范围规则,只要没有代码错误(例如,高于let的同名var声明之上的var声明将在范围内考虑,但这不是问题,因为这是语法错误)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42660523

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档