我试图做以下事情以满足代码构建器(特别是Sencha Cmd)的要求。
这就是我需要做的实质。关键因素在于函数体必须以对象文字的返回结束。由于构建器的限制,我无法返回变量。所以,如果参数'includeB'为true,如何在伪代码下面的点处添加属性'b',但如果为false,则完全不添加属性。即b==undefined或b==null是不允许的。
也许这是不可能的。
function create(includeB) {
// Can have code here but the final thing MUST be a return of the literal.
// ...
return {
a : 1
// pseudo code:
// if (includeB==true) then create a property called b
// and assign a value of 2 to it.
// Must be done right here within this object literal
}
}
var obj = create(false);
// obj must have property 'a' ONLY
var obj = create(true);
// obj must have properties 'a' and 'b'
感谢您的阅读和考虑,
Murray