0

I have this function right here which takes the parts of my object and illuminates them successively with a delay (it switches the opacity of an element to 1 and then switches the previous element opacity to 0 to illuminate them successively).

The problem is that I cannot use the this keyword to access the parent objects parts in the illuminateInternal function.

This hinders any possible reuse of my object since I will have maleBackNoZoom and maleBackFullZoom objects.

I don't want to change the variable names of the illuminateInternal function when I re-use my object so I would like to use something like the this keyword in the illuminateInternal function as well.

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);
}  
}
nicholaswmin
  • 21,686
  • 15
  • 91
  • 167
  • 1
    outside your `illuminateInternal` function define `var self = this` and call `self.parentFunctions` inside your inner function. – Dariel Pratama Jul 05 '14 at 12:21

1 Answers1

0

You can define local variable inside illuminate method which will store reference to its object. And then you can use it as alias of this.

var self = this;

illuminateInternal = function () {
    ..
    self.parts[property].opacity = 1;
}
hindmost
  • 7,125
  • 3
  • 27
  • 39