簡體   English   中英

如何獲得類財產?

[英]How to access to class property?

我有以下代碼:

function myClass() {

    this.tabs = new Array();

    myClass.prototype.focus_tab = function animateTab(nr){
        for(i=0;i<this.tabs.length;i++){
            $('#' + i + '-image').stop().animate(
                { left: '100px' },
                100 , function(){
                    this.tabs[i].step = 1;
                }
            );
        }
}

但是動畫末尾的功能無法識別“ this.tabs”。 怎么做好呢?

在不同的范圍內,請嘗試:

function myClass() {
  this.tabs = new Array();

  myClass.prototype.focus_tab = function animateTab(nr){
     for(i=0;i<this.tabs.length;i++){
         var mytab = this.tabs[i];
         $('#' + i + '-image').stop().animate({ left: '100px' }, 100 , function(){
             mytab.step = 1;
         }
      );
  }
}

還有其他一些問題,但是對該問題的評論已經解決了其中一些問題!

這是經典范圍界定問題的另一個示例。 您只有一個i變量,可以為所有回調共享。 您需要為每個回調設置一個本地i 將回調從以下位置更改:

function(){
    this.tabs[i].step = 1;
}

至:

(function(i){
    return function(){
        this.tabs[i].step = 1;
    }
})(i)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM