簡體   English   中英

嘗試將方法附加到類原型時,為什么代碼會中斷?

[英]Why does my code break when I try to attach my methods to the class prototype?

我正在嘗試創建一個用於切換視頻或圖像輪播的按鈕類。 每當我將方法附加到原型時,我的代碼都會失敗。

我正在嘗試使用“設計模式:揭示原型模式”

https://scotch.io/bar-talk/4-javascript-design-patterns-you-should-know

我的密碼筆: https : //codepen.io/Woodenchops/pen/QRzeXX?editors=0010

function playToggle(props) {

  console.log(this);

  this._parentElement = props.parentElement;
  this._button = props.button;
  this._playing = props.playing;
  this._props = props;
  this._mediaType = props.mediaType;
  this._video_id = props.videoID;
  this._caroursel_id = props.carouselID;

}

playToggle.prototype = function () {

// methods here

}()

(function () {

  var video = document.querySelectorAll('.container');

  videoArray = [];

  video.forEach(function (vid) {
    videoArray.push(new playToggle({
      parentElement: vid,
      button: '.playpause',
      playing: true,
      mediaType: 'video',
      videoID: '.myVideo'
    }))
  });

})();

我有控制台登錄this原型,並返回窗口對象來代替。 我也嘗試過使用bind其綁定到類-沒有運氣

您沒有正確定義原型

playToggle.prototype.thePrototypeName = function() {..}

 var play = new playToggle();
 play.thePrototypeName()

控制台日志結果

Object: playToggle {}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM