我有一个名为vars
的对象,它属于jQuery plugin function
。 vars
对象包含一堆HTML DOM类,供在类范围内使用。我的期望是将对象传递到类中,无论从作用域中调用它的位置如何,都可以访问它。
将vars
传递到新的类Proto
可以正常工作。问题是当我尝试从嵌套函数中获取vars
时,它不起作用(在我的情况下,尤其是touch methods
)。只能通过vars
和constructor
访问instance methods
。
这是代码:
(function ($, window, undefined) {
$.fn.plugin = function(options) {
return this.each(function() {
const that = $(this);
const vars = {
thatItem : 'thatItem'
// ...
}
var Proto = new className(options, vars);
Proto.exe(that);
})
// ...
}
class className {
constructor(options, globals) {
const defaults = {
value: 'this is a value'
}
this.options = Object.assign(defaults, options);
this.globals = globals;
console.log(this.globals, 'can be called using this keyword. works fine.');
}
exe(that) {
that.on({
touchstart:this.istouchStart,
touchmove:this.istouchMove,
touchend:this.istouchEnd
})
}
istouchStart(e) {
this.coordX = e.touches[0].clientX;
this.coordY = e.touches[0].clientY;
return this.coordX, this.coordY;
}
istouchMove(e) {
let dx = e.touches[0].clientX,
dy = e.touches[0].clientY,
dist = Math.sqrt(dx + this.coordX);
}
istouchEnd(e) {
var dist = e.changedTouches[0].clientX - this.coordX;
if (dist > 200) {
console.log(this.globals, "It doesn't work inside of the touch event.");
} else if (dist < -200) {
} else {
}
console.log(dist);
e.stopPropagation();
}
}
}(jQuery));
$('.inline-grid').plugin({})
* {
margin: 0;
padding: 0;
}
html, body
{
background-color: black;
}
div {
position: relative;
}
.inline-grid {
margin: 0 auto;
width: 560px;
border: 7px solid teal;
display: flex;
flex-flow: row;
overflow: hidden;
}
.wrap {
width: 100%;
display: flex;
flex-flow: row;
}
.cell {
display: flex;
flex-flow: column;
flex-shrink: 0;
width: 100%;
height: 200px;
}
.orange {
background-color: orange;
}
<div id="slider" class="inline-grid">
<div class="wrap">
<a href="#" class="cell orange">
</a>
<a href="#" class="cell blue">
</a>
<a href="#" class="cell crimson">
</a>
<a href="#" class="cell green">
</a>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
我将istouchEnd(e)
更改为istouchEnd(globals)
是为了直接给vars
作为参数。而且我使用了return
关键字来传递event
,但是这种方式也行不通。
istouchEnd(globals) {
return function(e) {
var dist = e.changedTouches[0].clientX - this.coordX;
if (dist > 200) {
console.log(this.globals, "It doesn't work when this.globals are inside of the nested function.");
} else if (dist < -200) {
} else {
}
console.log(dist);
}
e.stopPropagation();
}
如何在嵌套函数中获得vars
?
===============问题已更新==============
第一次尝试:
exe(that) {
that.on({
touchstart: (e) => {
this.coordX = e.touches[0].clientX;
this.coordY = e.touches[0].clientY;
return this.coordX, this.coordY;
},
touchmove: (e) => {
let dx = e.touches[0].clientX,
dy = e.touches[0].clientY,
dist = Math.sqrt(dx + this.coordX);
console.log('test');
},
touchend: (globals) => {
return function(e) {
var dist = e.changedTouches[0].clientX - this.coordX;
if (dist > 200) {
console.log(this.globals, "It doesn't work inside of the touch event.");
} else if (dist < -200) {
} else {
}
console.log(dist);
e.stopPropagation();
}
}
})
}