Titanium Javascript:“那个".不起作用 [英] Titanium Javascript: "that." does not work
问题描述
这个和那个都不行.有人知道这是怎么回事吗??
Neither this nor that works. Does anyone know what is going on??
qwerty 简称为qwerty();"在其他代码段中时.它应该是独立的.
qwerty is simply called as "qwerty();" when in other pieces of code. It is supposed to be indepedent.
我意识到出了什么问题.问题在于我...
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.
});
}
推荐答案
任何与 this
相关的 JS 问题很可能是由于函数使用 this
被调用.在您的 that
变量中存储对 this
的引用应该可以让您从嵌套函数中引用它,这正是您已经在做的方式 - 假设 qwerty()
的调用方式是首先将 this
设置为正确的对象.(我个人喜欢称这样的变量为 self
,因为它更准确地反映了变量在做什么.)
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
鉴于您说 this.chara[i].addEventListener(...)
我猜 chara[i]
变量持有对DOM 元素.如果是这种情况,我猜它是一种没有 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
这是我能为你做的最好的事情,但没有更多关于你得到什么错误以及如何调用 qwerty()
函数的信息......
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...
这篇关于Titanium Javascript:“那个".不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!