如何更新钛中的全局变量?

发布于 2024-12-06 15:17:28 字数 1065 浏览 1 评论 0原文

顺便说一句,我在更新全局数组时遇到一些问题。

这是我的代码:

Ti.App.dinercolor=["#FF5A00","#007EFF","#dccdc0","#C2FF95","#A700FD","#dccdc0","#dccdc0","#5F9EA0","#dccdc0","#dccdc0","#22A000","#DCCDC0","#dccdc0","#FF003C","#dccdc0","#FF003C","#dccdc0","#22A000","#dccdc0","#FFF191"];

这是我的全局数组,我可以从应用程序中的任何位置访问其中的数据。

当我想更新数组时,问题就出现了:

for(var q=0; q<Ti.App.dinercolor.length; q++){Ti.App.dinercolor[q] = '#dccdc0';}    

所以,完成操作后我期望的数组是这样的:

Ti.App.dinercolor=["#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0"];

但不知何故,我得到了相同的数组,但没有更新,

Ti.App.dinercolor=["#FF5A00","#007EFF","#dccdc0","#C2FF95","#A700FD","#dccdc0","#dccdc0","#5F9EA0","#dccdc0","#dccdc0","#22A000","#DCCDC0","#dccdc0","#FF003C","#dccdc0","#FF003C","#dccdc0","#22A000","#dccdc0","#FFF191"];

请帮助我,我没有知道我在这里做错了什么,

谢谢,

i'm having some problem in updating my array which is global by the way.

here is my code:

Ti.App.dinercolor=["#FF5A00","#007EFF","#dccdc0","#C2FF95","#A700FD","#dccdc0","#dccdc0","#5F9EA0","#dccdc0","#dccdc0","#22A000","#DCCDC0","#dccdc0","#FF003C","#dccdc0","#FF003C","#dccdc0","#22A000","#dccdc0","#FFF191"];

thats my global array which i can access data from it from anywhere in the application.

the problem comes when i want to update the array like:

for(var q=0; q<Ti.App.dinercolor.length; q++){Ti.App.dinercolor[q] = '#dccdc0';}    

so, the array i was expecting after the operation thats done is something like this:

Ti.App.dinercolor=["#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0","#dccdc0"];

but somehow i'm getting the same array with out updating,

Ti.App.dinercolor=["#FF5A00","#007EFF","#dccdc0","#C2FF95","#A700FD","#dccdc0","#dccdc0","#5F9EA0","#dccdc0","#dccdc0","#22A000","#DCCDC0","#dccdc0","#FF003C","#dccdc0","#FF003C","#dccdc0","#22A000","#dccdc0","#FFF191"];

please help me out, i have no idea what i'm doing wrong here,

Thank you,,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回忆那么伤 2024-12-13 15:17:28

您的代码是正确的,但您不应该扩展 Ti 对象,因为会发生这样的意外事情。创建您自己的对象,它就会起作用。

myObj = {};
myObj.dinercolor = [];

等等。

建议您将应用程序保留在单一上下文中,以便您可以从任何地方访问该对象。请观看锻造钛视频系列,了解一些最佳实践。

Your code is correct, but you shouldn't extend the Ti object as unexpected things like this will happen. Create your own object and it will work.

myObj = {};
myObj.dinercolor = [];

And so on.

It is recommended you keep your app in a single context so you will be able to access the object from anywhere. Check out the forging titanium video series for some best practices.

伴随着你 2024-12-13 15:17:28

我同意杰夫的观点,但是如果您希望上述方法起作用,您将需要更新整个数组,而不能只更新元素。

因此,将数组读入一个新变量,更新特定元素,然后再次设置属性

I agree with Jeff, however if you want the above approach to work you will need to update the whole array, you cannot just update elements.

So read the array out into a new variable, update the specific elements and then set the property again

始终不够爱げ你 2024-12-13 15:17:28

在 App.js 中:

Ti.App.my_variable = 0;

在 some_other_page.js 中:

Ti.App.my_variable = 101;

在 Yet_another_page.js 中:

alert( Ti.App.my_variable );

这将警报 101!

In App.js:

Ti.App.my_variable = 0;

In some_other_page.js:

Ti.App.my_variable = 101;

In yet_another_page.js:

alert( Ti.App.my_variable );

This will alert 101 !!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文