[英]Why does my code break when I try to attach my methods to the class prototype?
I'm trying to create a button class that toggles a video or image carousel. 我正在尝试创建一个用于切换视频或图像轮播的按钮类。 Whenever I attach my methods to the prototype, my code fails. 每当我将方法附加到原型时,我的代码都会失败。
I'm attempting to use the Design Pattern: Revealing Prototype Pattern 我正在尝试使用“设计模式:揭示原型模式”
https://scotch.io/bar-talk/4-javascript-design-patterns-you-should-know https://scotch.io/bar-talk/4-javascript-design-patterns-you-should-know
my code pen: https://codepen.io/Woodenchops/pen/QRzeXX?editors=0010 我的密码笔: 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'
}))
});
})();
I have console logged this
in the prototype, and it returns the window object instead. 我有控制台登录this
原型,并返回窗口对象来代替。 I have also tried using bind
to bind it to the class - with no luck 我也尝试过使用bind
其绑定到类-没有运气
You have not defined the prototype properly 您没有正确定义原型
playToggle.prototype.thePrototypeName = function() {..}
var play = new playToggle();
play.thePrototypeName()
Console Log Result 控制台日志结果
Object: playToggle {}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.