I am new to OOP Javascript and am trying to gain understanding on how it works by creating a simple effect. The issue I am encountering is that I cannot seem to reference variables from the constructor function inside an prototype transition function. Any help on this would be great. Thanks.
function AniSize( ele, nH, nW ) {
this.element = ele;
var origWidth = ele.width(),
origHeight = ele.height(),
newHeight = nH,
newWidth = nW;
}
AniSize.prototype.transition = function() {
this.element.hover(function() {
$(this).animate({
width: newWidth, // error: newWidth is not defined
height: newHeight // error: newHeight is not defined
});
}, function() {
$(this).animate({
width: origWidth, // error: origWidth is not defined
height: origHeight // error: origHeight is not defined
});
});
};
var box = new AniSize( $('div#box'), 200, 200 );
box.transition();
vardeclares a local variable, that won't be available outside of theAniSizescope, you need to attach them tothis..prototypeplay by all the same rules of variable scope. No magic I'm afraid.