Extract Method
Basics
When the Extract Method refactoring is invoked in the JavaScript context , WebStorm analyses the selected block of code and detects variables that are the input for the selected code fragment and the variables that are output for it.
The detected output variable is used as a return value for the extracted function.
Examples
Before | After |
---|---|
function multiplication(a,b) {
c = a + b;
d = c * c;
return d;
} |
function sum(a,b);
return a + b;
function multiplication(a,b) {
c = sum(a,b);
d = c * c;
return d;
} |
JavaScript example
Before | After |
---|---|
function multiplication(a,b) {
c = a + b;
d = c * c;
return d;
} |
function sum(a,b);
return a + b;
function multiplication(a,b) {
c = sum(a,b);
d = c * c;
return d;
} |
Extracting a function
To extract a function
- In the editor, select a block of code to be transformed into a function.
- On the main menu or on the context menu of the selection, choose or press Ctrl+Alt+M.
- In the Extract Function dialog box that opens, specify the name of the new function.
- To have the new function defined through a function expression, select the Declare functional expression check box.
- In the Parameters area, configure the set of variables to be passed to the new function as parameters. By default, all the variables from the specified scope are listed.
- View and check the declaration of the function to be generated in the Signature preview read-only area.
See Also
Reference:
Last modified: 11 July 2016