【发布时间】:2014-07-05 12:18:00
【问题描述】:
我在这里有这个函数,它获取对象的各个部分并延迟连续照亮它们(它将元素的不透明度切换为 1,然后将前一个元素的不透明度切换为 0 以连续照亮它们)。
问题是我无法使用 this 关键字访问 illuminateInternal 函数中的父对象部分。
这会阻碍我的对象的任何可能重用,因为我将拥有 maleBackNoZoom 和 maleBackFullZoom 对象。
当我重用我的对象时,我不想更改 illuminateInternal 函数的变量名,所以我也想在 illuminateInternal 函数中使用类似 this 关键字的东西。
maleBackNoZoom = {
parts:{
armsRight: findItemById("29") ,
armsLeft: findItemById("28"),
legsRight: findItemById("21"),
legsLeft: findItemById("22"),
back: findItemById("24"),
buttocks: findItemById("23"),
neck: findItemById("26"),
head: findItemById("27")
},
illuminate: function() {
var propertiesToIlluminate = [], prop, illuminateInternal, i = 0, delay = 200, intervalId;
for (prop in this.parts) {
propertiesToIlluminate.push(prop);
}
illuminateInternal = function () {
var property = propertiesToIlluminate[i];
var previousProperty = propertiesToIlluminate[i-1];
maleBackNoZoom.parts[property].opacity = 1;
console.log(previousProperty);
if (typeof previousProperty !== 'undefined'){
maleBackNoZoom.parts[previousProperty].opacity = 0;
}
paper.view.update();
i++;
if (i === propertiesToIlluminate.length) {
maleBackNoZoom.parts[property].opacity = 0;
clearInterval(intervalId);
setInterval(function(){paper.view.update()},delay);
}
};
intervalId = setInterval(illuminateInternal, delay);
}
}
【问题讨论】:
-
在您的
illuminateInternal函数之外定义var self = this并在您的内部函数中调用self.parentFunctions。
标签: javascript oop object this