我试图在对象中使用_.throttle
,但我认为我不知道如何正确执行此操作。在对象中使用下划线油门 - 如何保持对象的引用?
我使用jQuery和Underscore.js
function object() {
this.throtthled = function() {
// This should alert 1
alert(this.var);
}
this.fastFunc = _.throttle(this.throtthled, 100);
this.var = 1;
}
$(function() {
var myObject = new object();
$(document).on('mousemove', myObject.fastFunc);
});
但你可以在控制台上看到jsfiddle,这只回报undefined
。 我在做什么错?
您在创建它之前正在访问'this.throtthled'。 – Bergi 2014-12-01 20:30:24
你说得对。我修复了它,无法访问'this.var'。任何想法 ? – 2014-12-01 20:34:49
看到我的答案 – Bergi 2014-12-01 20:35:52