2011-05-03 100 views
0

在我的下列類定義中,當調用我的drawElipse方法時,我的寬度,高度,列等始終未定義。Javascript類屬性始終未定義

但是在我的頁面中,我設置了所有的屬性。

$(document).ready(function() { 
    var element = $('#positioning_canvas'); 
    element.click(scientificBox.click); 
    scientificBox.columns = 9; 
    scientificBox.rows = 9; 
    scientificBox.canvas = element.get(0); 
    etc. 

如何解決這個問題?

var scientificBox = new function() { 
var aliquotHeight; 
var aliquotWidth; 

this.width = 500; 
this.height = 500; 
this.columns = 9; 
this.rows = 9; 
this.canvas = null; 
this.imageRoot = ""; 

var drawElipse = function (ctx, x, y, w, h) { 
    var kappa = .5522848; 
    var ox = (w/2) * kappa; // control point offset horizontal 
    var oy = (h/2) * kappa; // control point offset vertical 
    var xe = x + w;   // x-end 
    var ye = y + h;   // y-end 
    var xm = x + w/2;  // x-middle 
    var ym = y + h/2;  // y-middle 
+1

我認爲你需要展示一些更多的代碼來使問題更加清晰;你可以使用jsfiddle或者jsbin來顯示所有頁面。無論如何,您是否嘗試過在您的doc-ready回調中將scientificBox記錄到控制檯並檢查它? – RwwL 2011-05-03 19:35:15

回答

2

在下面的代碼片段我創建了一個名爲ScientificBox有兩個屬性,一個方法類(我不太清楚,如果這是你所要求的解決方案)。

function ScientificBox() { 
     this.width = 9; 
     this.height = 9; 

     this.drawElipse = function() { 
      alert(this.width * this.height); 
     } 
    } 

你可以調用這個類,然後通過下面的代碼:

var box= new ScientificBox(); 
    box.drawElipse(); 

我測試過這一點,它的工作原理。也許這是你正在尋找的解決方案?

此致敬禮。

編輯:我忘了提及顯示在類上設置屬性的代碼示例。

var test = new ScientificBox(); 
    test.width = 12; 
    test.height = 2; 
    test.drawElipse(); 
+0

沒錯,不使用'new'就不會創建任何實例。應該是解決方案。 – 2011-05-04 06:44:45