Javascript类属性始终未定义

时间:2011-05-03 19:24:30

标签: javascript jquery class

在我的下面的类定义中,我的宽度,高度,列等在调用drawElipse方法时总是未定义。

然而,在我的页面中,我设置了所有属性。

$(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 个答案:

答案 0 :(得分: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();
相关问题