1

I am wondering if i can get the object property using array. I am getting 'undefine' value for this.label[i].xOffset. Any suggestions?

function label() {
    var lbl = this;
    lbl.rText;
    lbl.precision =1;
    lbl.prefix = '';
    lbl.suffix = '';
    lbl.xOffset = this.width/2;
    lbl.yOffset = this.height*82/100    
}
this.NoOfneedles=2;
if (this.NoOfneedles > 1) {
    this.label = [];
    for (i=0;i<this.NoOfneedles;i++) {
        this.label[i]=new label();
        alert("label xOffset:"+this.label[i]+this.label[i].xOffset);
    }
}

3 Answers 3

1

I think this may be the prob:

In your constructor function label() you are setting xOffset and yOffset to 'this' width and 'this' height, but 'this' doesn't have width/height properties so they are undefined.

Sign up to request clarification or add additional context in comments.

Comments

1

First, there's a problem in you label function:

lbl.xOffset = this.width/2;

Here, this.width is undefined, because this refers to the object you've create with new label(). But then, the xOffset property of the object would equal to NaN. And this line:

lbl.rText;

does nothing.

Comments

0

Your label object has no width and height properties and already others said you that but if you just define width and height inside your constructor then it could be written as follows

function Label() {
   var lbl = this;
   lbl.width=window.outerWidth;
   lbl.height=window.outerHeight;
   lbl.precision =1;
   lbl.prefix = '';
   lbl.suffix = '';
   lbl.xOffset = lbl.width/2;
   lbl.yOffset = lbl.height*82/100    
}

this.NoOfneedles=2;
if (this.NoOfneedles > 1){
    this.label = [];
    for (i=0;i<this.NoOfneedles;i++){
        this.label[i]=new Label();
        alert ("label xOffset:"+this.label[i].xOffset);
    }
}

Remember this refers to current object and you are using this.width/2; inside your constructor so this=Label and I've changed the label to Label and outside the constructor/in global scope this refers to default global object window and in the global scope this.someVar and someVar is same because window is the default global object and by default we don't need to use it so we can use document.getElementbyId instrad of window.document.getElementbyId.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.