萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

從面向過程到面向物件:用 JavaScript 實作拖曳效果的完整重構教學

教學目標

透過「拖曳效果」這個經典案例,示範如何將面向過程代碼逐步重構為面向物件(OOP)風格,理解 this 指向、原型鏈、封裝與複用的實務技巧。

第一階段:面向過程版本(基線)

window.onload = function() {
  var oBox = document.getElementById('box');
  var disX = 0, disY = 0;

  oBox.onmousedown = function(e) {
    e = e || window.event;
    disX = e.clientX - this.offsetLeft;
    disY = e.clientY - this.offsetTop;

    document.onmousemove = function(e) {
      e = e || window.event;
      oBox.style.left = (e.clientX - disX) + 'px';
      oBox.style.top  = (e.clientY - disY) + 'px';
    };

    document.onmouseup = function() {
      document.onmousemove = null;
      document.onmouseup = null;
    };
    return false; // 防止文字選取
  };
};

缺點:全域變數污染、難以建立多個獨立拖曳元素、邏輯與資料耦合。

第二階段:面向物件重構(ES5 建構函數 + Prototype)


// 建構函數
function Drag(id) {
  this.obj = document.getElementById(id);
  this.disX = 0;
  this.disY = 0;
}

// 原型方法
Drag.prototype.init = function() {
  var me = this; // 保存 this 參考
  this.obj.onmousedown = function(e) {
    e = e || event;
    me.mouseDown(e);
    return false;
  };
};

Drag.prototype.mouseDown = function(e) {
  var me = this;
  this.disX = e.clientX - this.obj.offsetLeft;
  this.disY = e.clientY - this.obj.offsetTop;

  document.onmousemove = function(e) {
    e = e || window.event;
    me.mouseMove(e);
  };
  document.onmouseup = function() {
    me.mouseUp();
  };
};

Drag.prototype.mouseMove = function(e) {
  this.obj.style.left = (e.clientX - this.disX) + 'px';
  this.obj.style.top  = (e.clientY - this.disY) + 'px';
};

Drag.prototype.mouseUp = function() {
  document.onmousemove = null;
  document.onmouseup = null;
};

// 啟動
window.onload = function() {
  new Drag('box').init();
  new Drag('box2').init(); // 秒建立第二個拖曳元素
};

重構關鍵對照表

面向過程面向物件優勢
全域變數 disX/disY實例屬性 this.disX多實例互不干擾
巢狀函數原型方法方法共享、記憶體節省
this 指向混亂var me = this 固定參考回呼函數中正確存取實例
事件綁定在 onload封裝在 init()初始化時機可控、易測試

ES6 Class 現代寫法(同邏輯、更清晰)


class Drag {
  constructor(id) {
    this.obj = document.getElementById(id);
    this.disX = 0;
    this.disY = 0;
    this.boundMouseMove = this.mouseMove.bind(this);
    this.boundMouseUp = this.mouseUp.bind(this);
  }

  init() {
    this.obj.addEventListener('mousedown', this.mouseDown.bind(this));
  }

  mouseDown(e) {
    this.disX = e.clientX - this.obj.offsetLeft;
    this.disY = e.clientY - this.obj.offsetTop;
    document.addEventListener('mousemove', this.boundMouseMove);
    document.addEventListener('mouseup', this.boundMouseUp);
  }

  mouseMove(e) {
    this.obj.style.left = (e.clientX - this.disX) + 'px';
    this.obj.style.top = (e.clientY - this.disY) + 'px';
  }

  mouseUp() {
    document.removeEventListener('mousemove', this.boundMouseMove);
    document.removeEventListener('mouseup', this.boundMouseUp);
  }
}

// 使用
new Drag('box').init();
new Drag('box2').init();

進階擴充建議

  • 邊界限制:在 mouseMove 中加入 Math.max(0, Math.min(maxX, ...)) 限制拖曳範圍。
  • 觸控支援:同步監聽 touchstart/move/end,讀取 e.touches[0].clientX。
  • 事件解耦:改用 CustomEvent 派發 dragstart/drag/dragend,供外部訂閱。

結語

從面向過程到面向物件,核心在於「將資料與操作資料的行為封裝在一起」。拖曳案例完美展示了 this 綁定、原型共享、多實例隔離等 OOP 精髓,是前端進階必練題目。

copyright © 萬盛學電腦網 all rights reserved