Javascript / Jquery OOP不继承属性

时间:2015-06-03 15:41:38

标签: javascript jquery oop constructor encapsulation

我创建了一个Constructor类/函数,它有4个方法和5个属性。问题是当我创建一个构造函数的新实例时,它不会继承第一个属性(this.el = element),它是' affiliateSection'在名为:animation1的新实例上。提前谢谢..

// main class
function AnimateImages(el, time, effect, setTime, h){

    this.el = element; // this is not inheriting from 'animate1'
    this.time = time;
    this.effect = effect;
    this.setTime = setTime;
    this.h = height;

    this.delayAnimate = function() {
        this.element.delay(time)
            .queue(function(next) {
                $(this).addClass(effect);
                next();
            });
    };

    // function for multi animations
    var multiAnimations = function() {
        var i = 0;
        this.element.each(function (key, value) {
            i = i + setTime;
            var tthis = this;
            delayAnimate($(this), i, tthis.effect);
        });
    };

    // load on window height
    var onWindowAnimate = function () {
        if ($(this).scrollTop() > this.height) {
            // call multi animations function
            var tthis = this;
            multiAnimations(this.element, tthis.setTime, tthis.effect);
        }
    };
    // hide function
    var hideAnimatedEl = function (){
        this.element.each(function(){

            $(this).css("visibility", "hidden");
        });
    };
} // End of AnimateImages

/*============================================================*/

 var affiliateSection = $("#aff-img > li > img");
 // new instance of AnimateImages
 var animation1 = new AnimateImages(affiliateSection, 200, 'subtlefadeIn', 
  300, 50);


 $(window).scroll(function () {
    setTimeout(function(){
        animation1.onWindowAnimate();

      }, 1000);
 });

2 个答案:

答案 0 :(得分:1)

看起来你的成员变量初始化是向后的。试试这个:

this.element = el; // this is not inheriting from 'animate1'
this.time = time;
this.effect = effect;
this.setTime = setTime;
this.height = h;

答案 1 :(得分:0)

您的参数名称错误:

this.el = element;

element不在参数列表中。

由于您在函数内部this.element,我假设您的第一行应该是

this.element = el;