如何让 titanium 允许为 JavaScript 函数设置默认参数值

How can I get titanium to allow setting a default parameter value for a JavaScript function

基本上,以下模式被拒绝并被视为错误。

function fooBar(flag = true) {
  // flag will be set to true is someone called fooBar() without any parameters
}

编辑:我添加了以下代码片段来展示实际操作。这更优雅,更易于阅读和维护,并且更快 运行 (无下划线要求)......并且它受到 JavaScript 的支持。

function greet(param = "World") {
 alert("Hello " + param + "!");
}

greet(); // Will alert "Hello World"
greet("Thomas"); // Will alert "Hello Thomas"

谢谢

与其说是 Titanium 问题,不如说是 javascript 问题。 您可以使用下划线库中的 isUndefined 函数(包括 Alloy):

function fooBar(flag) {
  // flag will be set to true is someone called fooBar() without any parameters
  if(_.isUndefined(flag))
    flag = true;

}

不要忘记 Titanium Mobile 使用 ECMAScript 5,ECMAScript 6 引入了函数的默认值参数:http://ariya.ofilabs.com/2013/02/es6-and-default-argument.html.