溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

VSCode中如何進行前端重構

發(fā)布時間:2022-03-24 09:17:55 來源:億速云 閱讀:173 作者:iii 欄目:軟件技術

這篇文章主要介紹“VSCode中如何進行前端重構”,在日常操作中,相信很多人在VSCode中如何進行前端重構問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”VSCode中如何進行前端重構”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

重命名

為什么要重命名:命名不清晰,無法讓人理解。

操作步驟:

  • 選中變量名,鼠標右鍵選擇重命名符號(Rename Symbol),或者使用快捷鍵 F2 ;

  • 彈出框輸入想要修改的名字;

  • VSCode 會把后續(xù)相關的名字都改掉。

重構操作

  • 選中要重構的內容,鼠標右鍵選擇重構(Refactor),或者使用 Ctrl + Shift + R。

  • 根據(jù)選中的內容,可能會出現(xiàn)以下內容供選擇重構:

    • generate ‘get’ and ‘set’ accessors 生成get、set處理器

    • 將函數(shù)轉換成 ES2015類

    • 將所有函數(shù)轉換成類

    • 提取到 class 'xxx' 中的 Method

    • 提取到 class 'xxx' 中的 readonly field

    • generate ‘get’ and ‘set’ accessors 生成get、set處理器

    • Extract function

    • 提取到當前函數(shù)里的 inner function

    • 提取到 Module 范圍的 function

    • 提取到 global 范圍的 function

    • Convert to template string 轉換成模板字符串

    • Extract constant

    • 提取到封閉范圍的 constant

    • 提取到 Module 范圍的 constant

    • Convert to optional chain expression

    • 刪除未使用的聲明

    • 在未使用的聲明前

    • Move to a new File

    • Convert default export to named export

    • Convert named export to default export

    • Convert namespace import to named export

    • Convert named imports to namepace export

    • import/export

    • 函數(shù)/類

    • 變量/表達式

    • 字符串

    • 表達式/函數(shù)

    • 對象方法

魔法數(shù)字

為什么要修改魔法數(shù)字?因為除進制數(shù)之外,數(shù)字的實際意義無法被人看懂。

目標:定義一個常量值,寫清楚改數(shù)字的實際意義。

操作:

  • 選中魔法數(shù)字進行重構。根據(jù)需要,推薦選擇:

    兩者都會定義一個常量,前者定義在當前函數(shù)內,后者則是整個模塊/文件中;

    • 提取到封閉范圍的 constant

    • 提取到 Module/global 范圍的 constant

  • 代碼抽取到新的變量中,并出現(xiàn)重命名的輸入框;

  • 使用全大寫單詞,單詞使用“_”間隔。

例子:今年雙十一持續(xù)13天,計算除雙十一促銷結束的時間。

function promotionEndDate() {
  return new Date(new Date('2022-11-11').getTime() + 13 * 60 * 60 * 24 * 1000);
}

/**
 * 修改后:
 * 將開始時間 START_DATE,持續(xù)的天數(shù) LASTING_DAYS 抽取出來做成變量
 * 如果只有一處使用,則在使用到的函數(shù)內定義;
 * 如果多處都有用,可以考慮放在函數(shù)外,模塊內。
 */
function promotionEndDate() {
    const START_DATE = '2022-11-11';
    const LASTING_DAYS = 13;
    return new Date(new Date(START_DATE).getTime() + LASTING_DAYS * 60 * 60 * 24 * 1000);
}

復雜的邏輯條件

為什么要修改復雜邏輯?復雜的邏輯,往往條件判斷繁多,閱讀難度比較高。

操作:

  • 選中復雜的邏輯條件進行重構。根據(jù)需要,選擇:

    • 提取到封閉范圍的 constant

    • 提取到當前函數(shù)里的 inner function

    • 提取到 Module/global  范圍的 function

  • 代碼抽離到一個新的變量/函數(shù)中,并出現(xiàn)重命名的輸入框;

  • 使用駝峰命名,使用 is/has 起頭,每個單詞首字母大寫。

例子:返回指定的某個月有多少天

function monthDay(year, month) {
    var day31 = [1, 3, 5, 7, 8, 10, 12];
    var day30 = [4, 6, 9, 11];
    if (day31.indexOf(month) > -1) {
        return 31;
    } else if (day30.indexOf(month) > -1) {
        return 30;
    } else {
        if ((year % 4 == 0) && (year % 100 != 0 || year % 400 == 0)) {
            return 29;
        } else {
            return 28;
        }
    }
}

/**
 * 修改后
 * 是否閏年在日期處理函數(shù)中會經(jīng)常使用,所以將其提取到當前模塊的最外層了
 */
function monthDay(year, month) {
    ...
    if (day31.indexOf(month) > -1) {
        return 31;
    } else if (day30.indexOf(month) > -1) {
        return 30;
    } else {
        if (isLeapYear(year)) {
            return 29;
        } else {
            return 28;
        }
    }
}

function isLeapYear(year) {
    return (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0);
}

寫了注釋的代碼片段

更推薦代碼即注釋的理念。我們寫注釋之前要想明白為什么需要注釋?

  • 如果代碼本身已經(jīng)很清晰,應該刪除注釋。

  • 如果抽取代碼片段,取個合適的名字,能讓代碼易于閱讀,也可以刪除注釋。

目標:將代碼片段抽取出來做成函數(shù),函數(shù)以此代碼塊的具體功能做命名。

操作:

  • 選擇代碼塊,重構(Refactor)。選擇:

    • 提取到當前函數(shù)里的 inner function

例子:ajax 請求

function ajax(options) {
  options = options || {};
  options.type = (options.type || 'GET').toUpperCase();
  options.dataType = options.dataType || 'json';
  const READY_STATE = 4;
  const NET_STATUS = {
    OK: 200,
    RIDERCT: 300
  };
  const params = this.formatAjaxParams(options.data);
  let xhr;

  // 創(chuàng)建 - 非IE6 - 第一步
  if (window.XMLHttpRequest) {
    xhr = new window.XMLHttpRequest();
  } else { // IE6及其以下版本瀏覽器
    xhr = new window.ActiveXObject('Microsoft.XMLHTTP');
  }

  // 連接 和 發(fā)送 - 第二步
  if (options.type === 'GET') {
    ...
  } else if (options.type === 'POST') {
    ...
  }
  
  // 接收 - 第三步
  xhr.onreadystatechange = function () {
    if (xhr.readyState === READY_STATE) {
      ...
    }
  };
}

// 修改后
function ajax(options) {
  ...
  let xhr;

  create();
  connectAndSend();
  recieve();

  function create() {...}
  function connectAndSend() {...}
  function recieve() {...}
}

過長的函數(shù)

功能拆分做成外部函數(shù),再在內部調用。

操作:

  • 選擇代碼塊重構,選擇:

    • 提取到 Module/Global  范圍的 function

  • 代碼塊會生成一個函數(shù),并攜帶必要的參數(shù)

例子:上個例子中,可以將 ajax 的接收模塊獨立成模塊的function

function ajax(options) {
  ...

  create();
  recieve();
  connectAndSend(options, xhr, params);
}
function connectAndSend(options, xhr, params) {
  if (options.type === 'GET') {
    ...
  } else if (options.type === 'POST') {
    ...
  }
}

重復的代碼/過長的文件

操作:

  • 選擇代碼塊重構,選擇 Move to a new file;

  • 代碼會遷移到以當前函數(shù)/類作為文件名的文件中;如果有多個類/函數(shù),會以第一個類/函數(shù)做命明

  • 將函數(shù)/類使用 export 暴露出去;

  • 在原文件中用 import 引入函數(shù)/類。

import/export

default 和命名、命名空間和命名的轉換。

// named
export function nextMonthDay(year, month) {}

// default
export default function nextMonthDay(year, month) {}

// namepace 
import * as refactor from './refactor';

// named
import { nextMonthDay } from './refactor';

對象方法

生成get、set處理器

const person = {
  age: 32
};

// 生成get、set處理器
const person = {
  _age: 32,
  get age() {
    return this._age;
  },
  set age(value) {
    this._age = value;
  },
};

模板字符串

字符串拼接,快速轉換成模板字符串:

class Person{
  constructor(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }
  getFullName() {
    return this.firstName + ' ' + this.lastName;
  }
}

// 模板字符串
class Person{
  constructor(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }
  getFullName() {
    return `${this.firstName} ${this.lastName}`;
  }
}

生成get、set處理器,與對象方法的結果類似。

提取到 class xxx 的 Method, 與上面寫注釋的代碼、重復代碼提取的類似。

在此不再復述。

提供 ES 2015 類轉換,支持原型方法轉換。

const Person = function() {
  this.age = 32;
};
Person.prototype.getAge = function() {
  return this.age;
}
Person.prototype.setAge = function(value) {
  return this.age = value;
}

// ES 2015 類
class Person {
  constructor() {
    this.age = 32;
  }
  getAge() {
    return this.age;
  }
  setAge(value) {
    return this.age = value;
  }
}

到此,關于“VSCode中如何進行前端重構”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI