问题描述
如果该实例不存在,有没有办法不从函数对象(它的实例)调用方法?
这是我的粘性侧边栏功能,它有两种方法; init() 和 updateSticky();
function stickySideBar(element,options = {}) {
var _this = this;
_this.init = function () {}
_this.updateSticky = function (timeout) {
//this is actually just a debouncer that calls init method
}
}
我想在调整窗口大小时使用这个 updateSticky
$(window).on("resize",function () {
newsletterBlog.updateSticky();
sideBarBlog.updateSticky();
if ($(productssticky.element).length > 0) {
productssticky.updateSticky();
}
});
现在我使用 if 循环来检查元素是否存在,但我想在该函数的实例内部执行此操作
如果我没有 if 循环,我会得到“Uncaught TypeError: e.updateSticky is not a function”。
干杯
编辑
这是函数
function stickySideBar(element,options = {}) {
var _this = this;
console.log("_this :>> ",_this);
//declared element
_this.debouncer;
_this.element = element;
if ($(_this.element).length === 0) {
return;
}
// declared options
_this.parentElementClass = options.parentElementClass;
_this.wrapClass = options.wrapClass;
_this.activeStickyClass = options.activeStickyClass;
_this.top = options.top;
_this.width = options.width;
_this.activeBottomClass = options.activeBottomClass;
_this.disableOnMobile = options.disableOnMobile ? options.disableOnMobile : true;
_this.breakpoint = 992;
_this.init = function () {
};
_this.updateSticky = function (timeout) {
if ($(_this.element).length === 0) return;
var timeoutVal = timeout ? timeout : 100;
clearTimeout(_this.debouncer);
_this.debouncer = setTimeout(function () {
_this.init();
},timeoutVal);
};
return _this.init();
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)