search

Home  >  Q&A  >  body text

javascript - js中子对象引用父对象的属性

var FocusScorll = (function(){

var FocusScorll = function(id){
    this._id = id;
    this.pullDownFlag = 0;
    this.pullUpFlag = 0;
    this.pullDown = document.getElementById("pullDown");
    this.pullUp = document.getElementById("pullUp");
    this.spinner = document.getElementById("spinner");
    this.myScroll = null;
    this.init();
}
FocusScorll.prototype.init= function(){
    console.log("id:" + this._id);
    this.myScroll = new IScroll("#"+this._id,{
        probeType: 3,
        mouseWheel: true,//鼠标滑轮开启
        scrollbars: true,//滚动条可见
        fadeScrollbars: true,//滚动条渐隐
        interactiveScrollbars: true,//滚动条可拖动
        shrinkScrollbars: 'scale', // 当滚动边界之外的滚动条是由少量的收缩
        useTransform: true,//CSS转化
        useTransition: true,//CSS过渡
        bounce: true,//反弹
        freeScroll: false,//只能在一个方向上滑动
        startX: 0,
        startY: 0,
    });
    
    this.myScroll.on('scroll',_this.positionJudge);
    this.myScroll.on("scrollEnd",_this.action);
};
FocusScorll.prototype.positionJudge= function(){
    if(this.y>40){    //判断下拉
        _this.pullDown.style.display="block";
        _this.pullDown.innerHTML = "松开刷新页面";
        _this.pullDownFlag = 1;
    }else if(this.y<(this.maxScrollY-40)){   //判断上拉
        _this.pullUp.style.display="block";
        _this.pullUp.innerHTML = "松开刷新页面";
        _this.pullUpFlag = 1;
    }
};
FocusScorll.prototype.action = function(){
     if(_this.pullDownFlag==1){
        _this.pullDownAction();
        _this.pullDown.innerHTML = "正在加载…";
        _this.pullDownFlag = 0;
    }else if(_this.pullUpFlag==1){
        _this.pullUpAction();
        _this.pullUp.innerHTML = "正在加载…";
        _this.pullUpFlag = 0;
    }
};
FocusScorll.prototype.pullDownAction= function(){
    News.listInitFocus();
    setTimeout(function(){
             $(".scrollcontent").height($(document).height()-90);
             _this.myScroll.refresh();
         _this.pullDown.style.display="none";
    },500);
};
FocusScorll.prototype.pullUpAction= function(){
     _this.getData();
    setTimeout(function(){
        _this.myScroll.refresh();
        _this.pullUp.style.display="none";
    },200);
};
FocusScorll.prototype.updatePosition= function(){
     _this.pullDown.innerHTML = this.y>>0;
};
FocusScorll.prototype.getData= function(){
     console.log("_id:" + _this._id);
        var id = $(".selected").text();
        var num =  parseInt($("#"+id+"num").val())+1;
        $("#"+id+"num").val(num);
        alert("num:" + num + ",id:" + id);
       $ajax({
            url:"/news/fenye", 
            data:{
                num:num
            }, 
            success:function(res){
             var item=res.result;
              var html = "";
              if(item.length<=0){
                  //
                  alert("已经是最后一页了!");
              }else{
                  $.each(item,function(j,news){
                    html+=News.getNewsHtml(news);
                    });
                }
                $("#" + id + "1").append(html);
                _this.myScroll.refresh();
                //_this.myScroll.scrollTo(0,0,100);
            },
            error:function(){
                alert("1");
            }
        });
};
return function(id){
    return new FocusScorll(id);
}

})();

iscroll在绑定scroll事件positionJudge的时候,因为是iscroll引用,所以在positionJudge中,this的引用是myscroll,请问我怎么在positionJudge中获取顶级对象的属性呢,请不要使用全局对象,因为我会调用多次,例如:

var c1 = new FocusScroll("c1");
var c2 = new FocusScroll("c2");
var c3 = new FocusScroll("c3");

如果用_this = this的话,这样_this获取的永远是c3,因为_this是全局对象

PHP中文网PHP中文网2819 days ago239

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-04-10 16:53:58

    我想问下题主说的顶级对象是哪个?
    FocusScorll这个函数的原型上附加了好多个属性,所以positionJudge是FocusScorll原型的一个方法

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-10 16:53:58

    想在this.myScroll的scroll回调中使用positionJudge方法
    可以使用如下:

    this.myScroll.on('scroll',this.positionJudge.bind(this));

    var _this=this;
    this.myScroll.on('scroll',function(){
        _this.positionJudge();
    });

    reply
    0
  • Cancelreply