在JetBrains IDE中重构内部作用域 [英] Refactoring inside scope in JetBrains IDE

查看:98
本文介绍了在JetBrains IDE中重构内部作用域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个函数内重构一个变量,但是只能在那个函数内重构.在JetBrains IDE中可以做到这一点吗?

I would like to refactor a variable inside a function, but only inside that function. Is this possible in the JetBrains IDE's?

示例:

var global = 0;

function func1 (val) {
    if (val === global) {
        doSomething();
    } else if (val * 2 === global) {
        doSomethingElse();
    } else {
        doSomethingElseEntirely();
    }
}

function func2 (val) {
    if (val === global) {
        doSomething();
    } else if (val * 2 === global) {
        doSomethingElse();
    } else {
        doSomethingElseEntirely();
    }
}

如果我尝试通过重构更改func1中的变量global,它将在所有globals范围内更改,在func2中也是如此.我想防止这种情况.这可能吗?

If I try to change the variable global inside func1 via refactor, it will be changed in all of globals scope, so in func2 as well. I would like to prevent this. Is this possible?

推荐答案

这是一个更简单的解决方法.

Here is a simpler workaround.

  1. func1内添加一个虚拟var global.
  2. 使用重构">重命名"工具( Shift + F6 ),同时在func1中选择任何出现的global.
  3. 删除步骤1中添加的行(将插入号放入行中,然后按 Ctrl + Y ).
  1. Add a dummy var global within func1.
  2. Use the Refactor > Rename tool (Shift + F6), while selecting any occurrence of global in func1.
  3. Remove the line (place the caret in the line and hit Ctrl + Y) added in step 1.

这篇关于在JetBrains IDE中重构内部作用域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆