2012-12-28 55 views
1

我不數組這個關鍵字,爲什麼我得到這個錯誤:中的物件進行篩選方法

錯誤是:無法獲取的值「0」:對象爲空或未定義

,這是我的代碼:

var Box = function(width, height, type){ 
var top = 0, 
    right = 0; 
this.width = width; 
this.height = height; 
this.position = { 
    top: top, 
    right: right, 
    set: function(top, right){ 
     this.top = top; 
     this.right = right; 
    } 
}; 
this.type = type; 
this.area = this.width * this.height; 
this.className = "box_" + this.width + "_" + this.height;}; 

    var box01 = new Box(2, 2, "slideShow"); 
    var box02 = new Box(2, 1, "clinics"); 
    var box03 = new Box(1, 2, "articles"); 
    var box04 = new Box(1, 1, "news"); 
    var box05 = new Box(2, 1, "news"); 
    var box06 = new Box(2, 1, "clinics"); 

    var boxArray = []; 
    boxArray.push(box01); 
    boxArray.push(box02); 
    boxArray.push(box03); 
    boxArray.push(box04); 
    boxArray.push(box05); 
    boxArray.push(box06); 



var BoxUtility = function(parentId) { 
this.parentId = parentId; 
this.boxList = Array.prototype.pop.apply(arguments); 
Object.defineProperties(this, { 
    totalArea: { 
     get: function(){ 
      var x = 0; 
      for(var i = 0, len = this.boxList.length; i <= len - 1; i++){ 
       x = x + this.boxList[i].area; 
      }; 
      return x; 
     } 
    }, 
});}; 


BoxUtility.prototype.positionCalculateMasonry = function(preDefinedRows, firstColumnWidth, restColumnWidth, itemIndex){ 
var firstColumnArea = preDefinedRows * firstColumnWidth, 
    restColumnArea = preDefinedRows * restColumnWidth, 
    firstColumnAreaRemained = firstColumnArea, 
    restColumnAreaRemained = restColumnArea, 
    Position = function(top, right){ 
     this.top = top; 
     this.right = right; 
     this.restFirstWidth = firstColumnWidth - right; 
     this.restSecondWidth = restColumnWidth - right; 
    }, 
    firstPosition = new Position(0, 0), 
    positionStack = [], 
    boxStack = [], 
    indexStack = []; 

positionStack.push(firstPosition); 

for(var i = itemIndex, len = this.boxList.length; i <= len - 1; i++){ 
    if(this.boxList[i].area <= firstColumnAreaRemained){ 
     var temp = positionStack.filter(function(el){ 
      return (el.restFirstWidth >= this.boxList[i].width); 
     }); 
    } else { 

    }; 
};}; 

var x = new BoxUtility(clinica ,boxArray); 
x.positionCalculateMasonry(5, 3, 2, 0); 

我得到這個錯誤在這裏:

return (el.restFirstWidth >= this.boxList[i].width); 

相信其對經過該鍵字中的過濾方法

有幫助嗎?感謝

+4

嘗試製作成'this'回調外篩選的參考。走着瞧吧。 – 0x499602D2

+0

感謝您的解決方案解決了它,但我們如何通過回調函數內部? –

+0

只需在'.filter'調用之前執行'var self = this;',並且在回調中看到'this'的地方將其替換爲'self'。 – 0x499602D2

回答

2
BoxUtility.prototype.positionCalculateMasonry = function(preDefinedRows, firstColumnWidth, restColumnWidth, itemIndex){ 
var firstColumnArea = preDefinedRows * firstColumnWidth, 
    restColumnArea = preDefinedRows * restColumnWidth, 
    firstColumnAreaRemained = firstColumnArea, 
    restColumnAreaRemained = restColumnArea, 
    Position = function(top, right){ 
     this.top = top; 
     this.right = right; 
     this.restFirstWidth = firstColumnWidth - right; 
     this.restSecondWidth = restColumnWidth - right; 
    }, 
    firstPosition = new Position(0, 0), 
    positionStack = [], 
    boxStack = [], 
    indexStack = [], 
    that = this; 

positionStack.push(firstPosition); 

for(var i = itemIndex, len = this.boxList.length; i <= len - 1; i++){ 
    if(this.boxList[i].area <= firstColumnAreaRemained){ 
     var temp = positionStack.filter(function(el){ 
      return (el.restFirstWidth >= that.boxList[i].width); 
     }); 
    } else { 

    }; 
};}; 

海報代碼的問題是上下文在回調中不可用。增加的內容是一個變量'that'來保存上下文。然後使用閉包將上下文傳遞到回調中。

+1

你能解釋你改變了什麼,爲什麼? – 0x499602D2

+0

謝謝,它的工作;) –

+0

增加了我改變,爲什麼。 – rissicay

1

this關鍵字將引用基於上下文的不同對象它被調用。

簡單的把錯誤表示boxListthis所引用的對象中定義

+0

感謝親愛的您的幫助,但這是整個範圍 –

+0

@KamranAsgari是的我剛發佈後才意識到。抱歉! –

1

filter函數內部this意味着與外部不同的東西,因爲函數本身就是一個對象方法。

你需要到外面this存儲在另一個變量,然後通過閉合交給filter功能:

// Outside the for loop 
var that=this; 
... 
// Inside filter() 
return el.restFirstWidth >= that.boxList[i].width; 
+0

感謝您的幫助 –