function main(){
return "foo".replace("o", "a")
}
标记表达式"foo".replace("o", "a")
或整行 → "提取到封闭作用域中的常量",将其命名为test
,函数将被重构为以下内容:
function main(){
const test = "foo".replace("o", "a")
return test
}
还有一个“快速修复”命令(Ctrl+.),可用于修复和重构。
如果你想为此操作定义额外的快捷方式,请在keybindings.json中手动定义(文档):
{
"key": "shift+ctrl+alt+r",
"command": "editor.action.codeAction",
"args": {
"kind": "refactor.extract.constant"
}
}
keybindings.json
文件,添加一个新的 "editor.action.codeAction"
条目,并且在 kind
中,VS Code 提供自动完成。通过输入 "refactor."
,你可以看到所有可能的重构操作 - 其中之一是 refactor.move.newFile
,这应该是你想要的操作。 - ford04