收藏
回答

this指向的问题

请问我怎么在构造函数中让this 指向这个函数本身呢?

回答关注问题邀请回答
收藏

3 个回答

  • 再见,电脑崽
    再见,电脑崽
    2018-09-05

    bind、apply、call了解一下

    2018-09-05
    有用 1
    回复 9
    • 星河
      星河
      2018-09-05

      具体点呗,平常的还有点头绪,但是小程续 的完全想不出


      2018-09-05
      回复
    • 再见,电脑崽
      再见,电脑崽
      2018-09-05回复星河

      把你代码段贴出来。

      2018-09-05
      回复
    • 星河
      星河
      2018-09-05回复再见,电脑崽

      function Dianaday(objDate) {

         var i, leap=0, temp=0;

         var baseDate = new Date(1900,0,31);

         var offset   = (objDate - baseDate)/86400000;

         this.dayCyl = offset+40;

         this.monCyl = 14;

         console.log(this)

         for(i=1900; i<2050 && offset>0; i++) {

            temp = lYearDays(i)

            offset -= temp;

            this.monCyl += 12;

         }

         if(offset<0) {

            offset += temp;

            i--;

            this.monCyl -= 12;

         }

         this.year = i;

         this.yearCyl=i-1864;

         leap = leapMonth(i); //闰哪个月

         this.isLeap = false;

         for(i=1; i<13 && offset>0; i++) {

            if(leap>0 && i==(leap+1) && this.isLeap==false){    //闰月

                --i; this.isLeap = true; temp = leapDays(this.year);}

            else{

               temp = monthDays(this.year, i);}

            if(this.isLeap==true && i==(leap+1)) this.isLeap = false;    //解除闰月

            offset -= temp;

            if(this.isLeap == false) this.monCyl++;

         }

         if(offset==0 && leap>0 && i==leap+1)

            if(this.isLeap){ this.isLeap = false;}

            else{this.isLeap=true;--i;--this.monCyl;}

         if(offset<0){offset+=temp;--i;--this.monCyl;}

         this.month=i;

         this.day=offset+1;

      }


      2018-09-05
      回复
    • 星河
      星河
      2018-09-05回复再见,电脑崽

      这是网上找的一个日历的代码一部分,我现在需要调用这个函数,同时需要向他自身赋值,

      2018-09-05
      回复
    • 再见,电脑崽
      再见,电脑崽
      2018-09-05回复星河

      自身赋值是指,你需要在这个函数内调用页面的this.setData()吗??

      2018-09-05
      回复
    查看更多(4)
  • 我二十一
    我二十一
    2018-09-06

    this指就是当前“执行的环境”,判定的方法很复杂,你可以看看“你不知道的JavaScript” 这一点小程序没有什么特别,至少我还没发现有什么特别。

    2018-09-06
    有用
    回复 1
    • 星河
      星河
      2018-09-06

      好的,谢谢

      2018-09-06
      回复
  • 2018-09-06

    1、首先 你找的日历算法已过时。

    2、可以在函数外 把this 赋值给变量  在以参数的形式传递给函数 。

    2018-09-06
    有用
    回复 1
    • 星河
      星河
      2018-09-06

      请问你有没有更好的日历算法,只要能获取到数据,包含阳历农历,节气,节日,阴历节日与阳历节日的

      2018-09-06
      回复
登录 后发表内容