Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
707 views
in Technique[技术] by (71.8m points)

object - Titanium Javascript: "that." does not work

Neither this nor that works. Does anyone know what is going on??

Edit: qwerty is simply called as "qwerty();" when in other pieces of code. It is supposed to be indepedent.

Edit: I realize what is wrong. The problem lies with the i...

function qwerty () {
..... for loop that changes i ......

var that = this;
this.chara[i] = createlabel.....

this.chara[i].addEventListener('click', function(e) {
    var j = e.source.id;
    alert("hello word");
    alert(this.chara[j].width); // I get the error here
});

this.chara[i].addEventListener('doubleclick', function(e) {
    alert("hello word");
    alert(that.chara[i].width); // I get the error here too.
});
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Any JS problem relating to this is likely due to the way the function using this is called. Storing a reference to this in your that variable should let you reference it from within your nested functions, exactly the way you are doing it already - assuming that qwerty() is called in a way that sets this to the correct object in the first place. (Personally I like to call such a variable self since it more accurately reflects what the variable is doing.)

However, in your function you say you get the error on this line:

that.chara[i].width

Given that you say this.chara[i].addEventListener(...) I'm guessing that the chara[i] variable holds a reference to a DOM element. If that is the case I'm guessing it is an element type that doesn't have a width property. Try this:

that.chara[i].style.width

https://developer.mozilla.org/en/CSS/width

That's the best I can do for you without more information about what error you're getting and how the qwerty() function is called...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...