与JS竞争“在定义之前使用”和Titanium Developer [英] Contending with JS "used before defined" and Titanium Developer

查看:68
本文介绍了与JS竞争“在定义之前使用”和Titanium Developer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个冗长的JavaScript文件传递JSLint,除了在定义之前使用错误。

I have a lengthy JavaScript file that passes JSLint except for "used before it was defined" errors.

我使用了正常的函数声明,如...

I used normal function declarations, as in...

function whatever() {do something;}

而不是......

var whatever = function(){do something;};

史蒂夫哈里森的回复到之前的帖子......

and consistent with Steve Harrison's reply to an earlier post...


<假设你用函数关键字声明了所有的
函数,我认为它是一个编程风格的
问题。就个人而言,我更喜欢
结构我的函数,其方式是
看起来是合乎逻辑的,并使代码尽可能读取为
。例如,
和你一样,我把一个初始化函数放在
的顶部,因为它是所有
起始的地方。

Assuming you declare all your functions with the function keyword, I think it becomes a programming-style question. Personally, I prefer to structure my functions in a way that seems logical and makes the code as readable as possible. For example, like you, I'd put an init function at the top, because it's where everything starts from.

...我喜欢按照对我有意义的顺序查看函数。

... I like seeing the functions in an order that makes sense to me.

脚本在几个测试的浏览器中完美运行(例如,FireFox,Safari,Mobile Safari,Fennec,IE,Chrome,Midori等)。

The script functions perfectly in the several browsers tested (e.g., FireFox, Safari, Mobile Safari, Fennec, IE, Chrome, Midori, etc.).

这是问题:我想使用里面的脚本使用Titanium构建的iPhone应用程序,但其编译器因使用前定义错误而停止。

Here's the problem: I want to use the script inside of an iPhone app being built with Titanium but its compiler stops with "used before defined" errors.

我怎样才能解决这个问题?

How can I get around this?

这可能是一个愚蠢的问题,但也......如果函数需要以特定顺序调用,如何解决需要回调最初调用它的函数的被调用函数的问题?例如......

This might be a stupid question but also... If functions need to be called in a particular order, how does one resolve the matter of a called function needing to call back to the function that originally called it? For instance...

function buildMenu(){
     Display a list of five menu items, each of which calls a function to build the associated screen.
}

function screen1() {
     Display the screen associated with menu item #1.
}

如果函数需要按顺序声明, function screen1 需要在函数buildMenu 之前。但是,如果在某些条件下,无法构建screen1并因此想重新显示菜单(即调用技术上尚未声明的函数),该怎么办?

If the functions need to be declared in order, function screen1 would need to precede function buildMenu. But what if, under certain conditions, screen1 cannot be built and hence wants to redisplay the menu (i.e., calling a function that is technically not yet declared)?

哦是的......还有一个问题:是否有网站或程序自动重新排序函数而不是要求程序员手动完成?

Oh yeah... one more question: Are there websites or programs that automatically re-sequence the functions rather than requiring the programmer to do it manually?

推荐答案

不,EM的答案不是正确的解决方案。尝试运行此JavaScript:

No, EM's answer is NOT the right solution. Try running this JavaScript:

(function () {
   foo(); // right

   var foo = function () {
     console.log("wrong");
   };

   foo(); // wrong

   function foo() {
     console.log("right");
   }

   foo(); // wrong
}());

这是因为解释器将首先读取函数声明,创建名称 foo 作为打印right的函数,然后读取 var 语句,并发现已经有一个名称 foo 所以它会跳过创建一个值为 undefined 的新变量,正常情况下会发生。然后它逐行处理代码,其中包括对 foo 的赋值。函数声明不会被重新处理。也许这在Titanium中会有不同的表现,但是在Firebug中尝试这个,你会得到我得到的。

This is because the interpreter will first read the function declaration, create the name foo as a function that prints "right", then reads the var statement, and find that there is already a name foo so it will skip creating a new variable with the value undefined, as normally happens. Then it processes the code, line-by-line, which includes an assignment to foo. The function declaration does not get reprocessed. Maybe this will behave differently in Titanium, but try this in Firebug and you'll get what I got.

更好的解决方案是:

var screen1, buildMenu;

screen1 = function () { buildMenu(); };
buildMenu = function () { screen1(); };

这也将传递JSLint,并产生正确的行为。

This will also pass JSLint, and produce the correct behavior.

这篇关于与JS竞争“在定义之前使用”和Titanium Developer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆